簡體   English   中英

SQL將行分組為列

[英]SQL Grouped Rows as Column

我需要更改分組行為列的幫助。

ColGroup      Code            
===================
1        A001
1        A001
1        A001   
2        A002
2        A002
3        A003

1         2       3
===================
A001    A002    A003
A001    A002 
A001 

感謝您的任何幫助。

您可以使用條件聚合來做到這一點:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM