22,300
社区成员




declare @tb table(序号 int,编码 varchar(10),发生次数 int, 不良原因 varchar(100))
insert into @tb
select 1,'A',2,'原因1' union all
select 2,'A',1,'原因2' union all
select 3,'A',1,'原因3' union all
select 4,'B',183,'原因2' union all
select 5,'B',49,'原因5' union all
select 6,'B',22,'原因6' union all
select 7,'C',22,'原因2' union all
select 8,'C',12,'原因3' union all
select 9,'C',8,'原因4'
select 编码,STUFF((select ','+tb2.不良原因 from @tb tb2 where tb1.编码=tb2.编码 order by tb2.序号 for xml path('')),1,1,'') as '不良原因合并' from @tb tb1 group by 编码
select 编码,stuff((Select ',' + s.不良原因 ,FROM aa s where t.编码 = s.编码 FOR XML PATH('')),1,1,'') as 不良因
from aa t
group by 编码