码迷,mamicode.com
首页 > 数据库 > 详细

SQL 分组后拼接字符串

时间:2014-06-28 17:20:21      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   strong   width      

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 分组后拼接字符串,布布扣,bubuko.com

SQL 分组后拼接字符串

标签:style   blog   color   strong   width      

原文地址:http://www.cnblogs.com/gguozhenqian/p/3797273.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!