-3

我有一张像这样的桌子:

ID   value
3      1
3      2
3      3
3      4
3      5
3      6

谁能帮我将此表转换为格式:

ID  Value
3   1,2,3,4,5,6
4

1 回答 1

1
select distinct id, 
stuff((select (','+[value]) from Table1 as t2 where t1.id=t2.id 
for xml path('')),1,1,'') as [values]
from 
    Table1 as t1  

SQLFIDDLE

PS: in example scheme there is varchar() type for values, you will need to convert numeric to varchar() if needed.
There are plenty variants to make group_concat in Sql Server, just another way with assembly: GROUP_CONCAT string aggregate for SQL Server. I found it pretty nice working.
Another way with COALESCE

DECLARE @values varchar(100)

SELECT @values = COALESCE(@values + ', ', '') + [value]
FROM Table1

SELECT @values  

SQLFIDDLE

于 2013-07-12T07:32:29.083 回答