晨风

-------------------- 业精于勤,荒于嬉;行成于思,毁于随

导航

SQL字符串分组聚合(分组后的数据查询后用逗号隔开)

Posted on 2015-01-04 15:38  shenyixin  阅读(1078)  评论(0编辑  收藏  举报
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go

select id, [value] = stuff((select ',' + [value] from tb t where id = tb.id for XML path('')) , 1 , 1 , '')
from tb
group by id