Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Combining data set onto one column

Author  Topic 

benking9987
Posting Yak Master

124 Posts

Posted - 2011-11-18 : 17:17:33
I have a set of data that looks like this:


Column1, Column2
Product1, A
Product1, B
Product1, C
Product1, D
Product1, E
Product2, G
Product2, H
etc.

I want to manipulate it so it looks like this:

Column1, Column2
"Product1", "A,B,C,D,E"
"Product2", "G,H,etc..."

Any ideas?

Thanks in advance.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-19 : 02:03:09
[code]
SELECT t.Column1,STUFF((SELECT ',' + Column2 FROM Table1 WHERE Column1 = t.Column1 ORDER BY Column2 FOR XML PATH('')),1,1,'') AS column2
FROM (SELECT DISTINCT Column1 FROM Table1)t
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -