将结果值中的某一字段数据GROUP BY以后合并到一个字段

原始数据
id    values
1        A
1        B
1        C
2        D
2        E
要得到的结果:
1    A,B,C
2    D,E 

--创建测试表、数据
create table tb(id int, value varchar(10)) 
insert into tb values(1, 'A') 
insert into tb values(1, 'B') 
insert into tb values(1, 'C') 
insert into tb values(2, 'A') 
insert into tb values(2, 'B') 
go 
-- 查询处理 
select id,stuff((select '+'+value from tb b where a.id=b.id order by value for xml path('')),1,1,'')
from tb a
group by id

--删除测试表
drop table tb 
posted @ 2014-10-14 09:49  giveup  阅读(993)  评论(0)    收藏  举报