Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在sql server中按列顺序在一行中汇总多行_Sql_Sql Server_String Aggregation - Fatal编程技术网

在sql server中按列顺序在一行中汇总多行

在sql server中按列顺序在一行中汇总多行,sql,sql-server,string-aggregation,Sql,Sql Server,String Aggregation,如何将具有相同值的多行以逗号分隔并按升序组合在一列中的方式返回到一行中(在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 根据c

如何将具有相同值的多行以逗号分隔并按升序组合在一列中的方式返回到一行中(在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

您正在使用哪些RDM?看起来你没有使用表2?可能是现在的重复。我想,我已经更新了这个问题:)你的rdbms是什么?Sql Server、postgres、oracle?什么版本?rdbms是SQLserver@SQL_NewUser它对我有用。也许您需要desc排序顺序,在这种情况下,您可以进行以下更改(按Col3 desc排序)。