Here's one way:;with yourTable (ID, DXCode)as( select 1, 47 union all select 1, 25 union all select 1, 17 union all select 1, 20 union all select 2, 87 union all select 2, 23 union all select 2, 92)select yt.[ID], stuff(max(x.DX), 1, 1, '') DXcodesfrom yourTable ytcross apply ( select ',' + convert(varchar(50), DXCode) from yourTable where [ID] = yt.[ID] order by DXCode for xml path ('') ) x (DX)group by yt.[ID]OUTPUT:ID DXcodes----------- ----------------1 17,20,25,472 23,87,92
Be One with the OptimizerTG