0

Please help with SQL Query to solve following issue.

I have a table input values like this - Column header is X and Y

    x | y
-------------------
    1 | a1
    1 | a2
    2 | a3
    2 | a4
    2 | a2

I need output like this

x | y
----------
1 |a1,a2
2 |a3,a4,a2

Please advice!

Dhana
  • 1,618
  • 4
  • 23
  • 39
  • 1
    check [this](http://social.msdn.microsoft.com/Forums/en/transactsql/thread/f09d4166-2030-41fe-b86e-392fbc94db53) as well. – Darshana Jun 05 '12 at 09:58

1 Answers1

1
SELECT nto.x, STUFF((SELECT CAST(',' AS varchar(max)) + nti.y
         FROM mytable AS nti WHERE nti.x = nto.x
         FOR XML PATH('')), 1, 1, '') AS Y
FROM mytable AS nto
GROUP BY nto.x
Darshana
  • 2,462
  • 6
  • 28
  • 54