SQL Query to get aggregated result in comma separators along with group by column in SQL Server
You want to use FOR XML PATH
construct:
select
ID,
stuff((select ', ' + Value
from YourTable t2 where t1.ID = t2.ID
for xml path('')),
1,2,'') [Values]
from YourTable t1
group by ID
The STUFF
function is to get rid of the leading ', '
.
You can also see another examples here:
- SQL same unit between two tables needs order numbers in 1 cell
- SQL and Coldfusion left join tables getting duplicate results as a list in one column