34,873
社区成员
发帖
与我相关
我的任务
分享
if OBJECT_ID('tb') is not null
drop table tb
go
create table tb(c1 int,c2 int)
insert into tb
select 1, 2 union all
select 1, 3 union all
select 1, 4 union all
select 2, 2 union all
select 2, 3 union all
select 2, 5
select c1,c2=STUFF((select ','+ltrim(c2)
from tb
where c1=a.c1 for xml path('')),1,1,'')
from tb a
group by c1
c1 c2
1 2,3,4
2 2,3,5
select
c1,
c2=stuff((select ','+ltrim(c2) from tb where c1=t.c1 for xml path('')),1,1,'')
from tb t
group by c1