SQL字符串分组聚合(分组后的数据查询后用逗号隔开)
SQL2005中的方法
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
转自:
本文介绍了一种在SQL Server 2005中实现字符串分组聚合的方法,通过使用STUFF函数结合FOR XML PATH,可以将同一组内的值用逗号连接起来。此技巧对于需要汇总分组数据的应用场景非常有用。

971

被折叠的 条评论
为什么被折叠?



