Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Come mostrare più valori di riga in virgola separati in una singola riga in SQL Server 2005?

select min(ID) as ID,
       Val,
       stuff((select ','+Cat
              from Table2 as T2
              where T1.Val = T2.Val
              for xml path(''), type).value('.', 'nvarchar(max)'), 1, 1, '') as Cat
from Table2 as T1
group by Val
order by ID

SQL Fiddle