Skip to content

Instantly share code, notes, and snippets.

@jasonicarter
Last active January 3, 2016 12:19
Show Gist options
  • Save jasonicarter/8461815 to your computer and use it in GitHub Desktop.
Save jasonicarter/8461815 to your computer and use it in GitHub Desktop.
Merge multiple rows into 1 row with column being concatenated into comma separated string. SQL
select column1,
stuff((SELECT distinct ', ' + cast(column2 as varchar(10))
FROM table t2
where t2.column1 = t1.column1
FOR XML PATH('')),1,1,'')
from table t1
group by column1
/*
column1 - field you want to group items based on
column2 - field you want concatenated
table t1, t2 - same table
http://stackoverflow.com/questions/12645297/merge-multiple-rows-into-one-column-without-duplicates/12645346?noredirect=1#comment22037856_12645346
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment