日期:2014-05-18 浏览次数:20642 次
select B , C=
stuff((select ','+C from tablename t where B =tablename.B for xml path('')), 1, 1, '')
from tablename
group by B
------解决方案--------------------
declare @tb table (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')
select id , [ccname]=
stuff((select ','+[value] from @tb t where id =tv.id for xml path('')), 1, 1, '')
from @tb as tv
group by id
/*
id ccname
1 aa,bb
2 aaa,bbb,ccc
*/