![](/img/trans.png)
[英]Rolling up multiple rows into a single row and column for SQL Server data
[英]rolling up multiple rows in a single row in a colum order in sql server
如何將具有相同值的多行返回為一行,並以逗號分隔並在單個列中按升序組合(在SQL Server中)?
表2
------------
col1 | col2 | col3
----------------------
1 | line1 | 2
1 | line2 | 1
1 | line3 | 4
2 | line4 | 1
2 | line5 | 3
2 | line6 | 2
3 | line7 | 2
3 | line8 | 1
按照col3的升序排列的所需結果:
Col1 | col2
----------------------------
1 | Line2,Line1,Line3
2 | Line4,Line6,Line5
3 | Line8,Line7
希望這是您要尋找的:
select Col1, string_agg(Col2, ',') within group(order by Col3)
from Table2
group by Col1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.