sql server - How to do it without the while loop in sql -
i have table this,
a | b ---------------------- 1 | 10 1 | 20 2 | 30 2 | 40
i need output as,
a | b ------------------------ 1 | 10,20 2 | 30,40
thank in advance
try this.
create table #table2( col1 int, col2 varchar(10), ) insert #table2 select 1, '10' union select 1, '20' union select 1, '30' union select 2, '40' union select 2, '50' union select 2, '60' select col1, col2 = stuff(( select ', ' + t2.col2 #table2 t2 t2.col1 = t1.col1 xml path(''), type).value('.', 'varchar(max)' ), 1, 2, '') #table2 t1 group t1.col1
Comments
Post a Comment