如何将具有相同值的多行返回为一行,并以逗号分隔并在单列中按升序组合(在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
答案 0 :(得分:1)
希望这是您想要的:
select Col1, string_agg(Col2, ',') within group(order by Col3)
from Table2
group by Col1