标签:sql字符串合并
create table #tb(id int, value varchar(10),cname varchar(20))
insert into #tb values(1, ‘aa‘,‘aaaa‘)
insert into #tb values(1, ‘bb‘,‘eeee‘)
insert into #tb values(1, ‘aa‘,‘tttt‘)
insert into #tb values(2, ‘aaa‘,‘gggg‘)
insert into #tb values(2, ‘bbb‘,null)
insert into #tb values(2, ‘ccc‘,‘hhhh‘)
insert into #tb values(1, ‘cc‘,‘llll‘)
go
----
---去重 select id, [value] = stuff((select distinct ‘,‘ + [value] from #tb t where id = #tb.id for xml path(‘‘)) , 1 , 1 , ‘‘),max(isnull(cname,‘‘)) from #tb group by id ---不去重 select id, [value] = stuff((select ‘,‘ + [value] from #tb t where id = #tb.id for xml path(‘‘)) , 1 , 1 , ‘‘),max(isnull(cname,‘‘)) from #tb group by id
drop table #tb
本文出自 “bamboo” 博客,请务必保留此出处http://hdf007.blog.51cto.com/42396/1920683
标签:sql字符串合并
原文地址:http://hdf007.blog.51cto.com/42396/1920683