type value
a 0
b 1
c 2
d 3
a 2
a 3
d 1
b 0
c 0
得到结果:
type value
a 0,2,3
b 1,0
c 2,0
d 3,1
select [type], stuff((select ','+ltrim(value) from tb where [type]=[type] for xml path('')),1,1,'') [value] from tb group by [type];