代码
with t as(
select 1 id, 'Child1' child
union
select 1 id, 'Child2'
union
select 1 id, 'Child3'
union
select 2 id, 'Child1'
union
select 2 id, 'Child2'
)
select id, stuff((select ','+child
from t a
where b.id = a.id
for xml path('')),1,1,'') children
from t b group by id
select 1 id, 'Child1' child
union
select 1 id, 'Child2'
union
select 1 id, 'Child3'
union
select 2 id, 'Child1'
union
select 2 id, 'Child2'
)
select id, stuff((select ','+child
from t a
where b.id = a.id
for xml path('')),1,1,'') children
from t b group by id