你可以帮助我一个案例,在
下面我有一张原始表格,如下:
+++++++++++++++++++++++++++
Col1 | Col2
+++++++++++++++++++++++++++
A | 1
A | 2
A | 3
B | 4
B | 5
然后我想要一个结果是这样的:
+++++++++++++++++++++++++++
Col1 | Col2
+++++++++++++++++++++++++++
A | 1,2,3
B | 4,5
我应该如何在SQL Server中执行此操作?
答案 0 :(得分:1)
select distinct t.col1, (
SELECT STUFF(
(
SELECT ',' + convert(varchar(10),col2)
FROM TABLE
where col1 = t.col1
FOR XML PATH('')
), 1, 1, '')
) col2
from TABLE t