Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I need a query for below for sql server 2000.

table structure:

col1 Col2
1 a
2 b
1 c
2 d
1 e
3 f

Out put should be like this:

col1 col2
1 a,c,e
2 b,d
3 f

Help me on this.
Posted

 
Share this answer
 
Comments
Espen Harlinn 1-Oct-11 13:18pm    
I really enjoyed your profile - it's a shame I can't vote for it too :)
Kim Togo 2-Oct-11 11:17am    
Haha thanks :-)
Must confess that the original text is her:

http://www.codeproject.com/Lounge.aspx?msg=3485467#xx3485467xx
Try this.
SQL
SELECT   Col1, LEFT([Col2], LEN([Col2]) - 1) as [Col2]
           FROM  ( SELECT  DISTINCT Col1
                      FROM  tablename )  AS A

OUTER APPLY ( SELECT  RTRIM(B.Col2)  + ', '
                FROM  tablename B WHERE A.Col1 = B.Col1
                 FOR  XML PATH('') ) AS C ([Col2])
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900