请帮助SQL Query解决以下问题。
我有一个像这样的表输入值 - 列标题是X和Y
x | y
-------------------
1 | a1
1 | a2
2 | a3
2 | a4
2 | a2
我需要这样的输出
x | y
----------
1 |a1,a2
2 |a3,a4,a2
请指教!
答案 0 :(得分: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