-3

i have a table like:

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

Can anyone help me to convert this table into format:

ID  Value
3   1,2,3,4,5,6
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
WNQs
  • 1
  • 1

1 Answers1

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

revoua
  • 2,044
  • 1
  • 21
  • 28