sqlserver-group by拼接字符串

with
temp00 as (
select
'1' id,
'zhangsan' name,
'gaoshu' cla,
'3' num  
select
'2' id,
'lisi' name,
'gaoshu' cla,
'2' num
union all
select
'2' id,
'lisi' name,
'jingji' cla,
'4' num
union all
select
'3' id,
'wangwu' name,
'gaoshu' cla,
'3' num
union all
select
'3' id,
'wangwu' name,
'wuli' cla,
'3' num )


select
id,
stuff(
(select ',' + name from  temp00 where id = a.id for xml path(''))
,1,1,'') as names --这个函数
from temp00 a
group by
id

posted @ 2020-05-26 17:35  林远  阅读(4673)  评论(0)    收藏  举报