两列数据:
id value
1 王
2 赵
3 李
4 张
1 姚
2 庄
得出效果:
1 王,姚
2 赵,庄
3 李
4 张
SQL:
select id, [value] = stuff((select ',' + [value] from tb t where id = tb.id for xml path('')) , 1 , 1 , '')
from tb
group by id
两列数据:
id value
1 王
2 赵
3 李
4 张
1 姚
2 庄
得出效果:
1 王,姚
2 赵,庄
3 李
4 张
SQL:
select id, [value] = stuff((select ',' + [value] from tb t where id = tb.id for xml path('')) , 1 , 1 , '')
from tb
group by id