I'm looking for a way to group whilst also concatenating rows into a comma separated string. Example:
Name Place
Steve Jones New York
Steve Jones Washington
Albert Smith Miami
Albert Smith Denver
to ...
Steve Jones New York, Washington
Albert Smith Miami, Denver
Greatly appreciated
If you use SQL Server 2008 and above, you can use STUFF and XML PATH to get the result you want.
SELECT DISTINCT Name
, STUFF((
SELECT ',' + Place
FROM YourTable t1
where t1.Name = t2.Name
FOR XML PATH('')
), 1, 1, '') AS Places
FROM YourTable t2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With