在sql server中显示单行到多列

时间:2022-06-21 13:16:44

I have one table like below

我有一张如下表

ID | Data | IDS
1  |  s1  |  4
2  |  s2  |  4
3  |  s3  |  4
4  |  d1  |  5
5  |  d2  |  5

I need Data to display column like USING IDS COLUMN DATA

我需要数据来显示像USING IDS COLUMN DATA这样的列

Data1 | Data2
s1    |  d1
s2    |  d2
s2    |  null

Please suggest on this requirement

请就此要求提出建议

1 个解决方案

#1


0  

You can do this using row_number() and group by:

您可以使用row_number()和group by来执行此操作:

select max(case when ids = 4 then data end) as col1,
       max(case when ids = 5 then data end) as col2
from (select t.*, row_number() over (partition by ids order by id) as seqnum
      from t
     ) t
group by seqnum;

#1


0  

You can do this using row_number() and group by:

您可以使用row_number()和group by来执行此操作:

select max(case when ids = 4 then data end) as col1,
       max(case when ids = 5 then data end) as col2
from (select t.*, row_number() over (partition by ids order by id) as seqnum
      from t
     ) t
group by seqnum;