[英]SQL Grouped Rows as Column
I need help to change grouped row as column. 我需要更改分组行为列的帮助。
ColGroup Code
===================
1 A001
1 A001
1 A001
2 A002
2 A002
3 A003
Into 成
1 2 3
===================
A001 A002 A003
A001 A002
A001
Thank you for any help. 感谢您的任何帮助。
You can do this using conditional aggregation: 您可以使用条件聚合来做到这一点:
select max(case when colgroup = 1 then code end) as [1],
max(case when colgroup = 2 then code end) as [2],
max(case when colgroup = 3 then code end) as [3]
from (select t.*,
row_number() over (partition by colgroup order by (select null)) as seqnum
from t
) t
group by seqnum
order by seqnum;
我已经尝试过了。我没有得到确切的结果,但这可能对您有所帮助http://www.sqlfiddle.com/#!9/4ecf6c/4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.