Forum Posts

Aggregate to CSV

Sometimes we need to rollup string values into lists of comma separated values. Using “FOR XML PATH” per column that you want to rollup can provide a way to do this.

This example was created based on work in this thread:
http://www.sqlservercentral.com/Forums/Topic802508-1672-1.aspx

Let’s say you have a data set like so:

id
fruit
city

101
Apple
Cleveland

101
Apple
Pittsburgh

101
Banana
Pittsburgh

102
Grape
Cleveland

102
Melon
Cleveland

103
Melon
Pittsburgh

103
Melon
Cleveland

and for each distinct “id”, you want [...]