我有一个这样的表:
ID Name ---------- 1 john 1 molly 2 greg 2 sean 1 holly 2 mill
SQL查询应如何汇总以下结果:
ID Name ------------- 1 john/molly/holly 2 greg/sean/mill
注意:STUFF函数只是从返回的字符串中删除前导/。
SELECT t1.id, STUFF((SELECT '/' + t2.name FROM YourTable t2 WHERE t1.id = t2.id ORDER BY t2.name FOR XML PATH('')),1,1,'') AS Name FROM YourTable t1 GROUP BY t1.id