复制代码
with t  as(
select 'Charles' parent, 'William' child union
select 'Charles', 'Harry' union
select 'Anne', 'Peter' union
select 'Anne', 'Zara' union
select 'Andrew', 'Beatrice' union
select 'Andrew', 'Eugenie' 
)

SELECT parent, 
STUFF(
     ( 
      SELECT ','+ child FROM t a WHERE b.parent = a.parent FOR XML PATH('')
     ),1 ,1, '') children 
FROM t b 
GROUP BY parent
复制代码

 

原表:

Parent
Child
Charles William
Charles Harry
Anne Peter
Anne Zara
Andrew Beatrice
Andrew Eugenie

 

处理的结果: 

 

Parent
Children
Charles William,Harry
Anne Peter,Zara
Andrew Eugenie,Beatrice

 
 
标签: Sql
posted on 2018-06-03 17:58  赵羴  阅读(133)  评论(0编辑  收藏  举报