34,593
社区成员
发帖
与我相关
我的任务
分享
declare @tab table(id varchar(10),val int)
insert into @tab
select 'A', 10 union
select 'A' , 11 union
select 'A' , 12 union
select 'B' , 20 union
select 'B' , 21 union
select 'B' , 22 union
select 'C' , 30 union
select 'C' , 31
select id,val=stuff((select ','+cast(val as varchar) from @tab where id=t.id for xml path('')),1,1,'')
from @tab t group by id
select a.dah,STUFF((select ','+CONVERT(varchar(10),fid) from tb1 where dah=a.dah for xml path('')) ,1,1,'')
from tb1 a group by a.dah
create table tb1(dah varchar(10),fid int)
insert into tb1 select 'A',10
union all
select 'A',11
union all
select 'A',12
union all
select 'B',20
union all
select 'B',21
union all
select 'B',22
union all
select 'C',30
union all
select 'C',31
create function joinstr (@dah varchar(10)) returns varchar(1000)
as
begin
declare @sql varchar(100)
set @sql=''
select @sql=@sql+','+convert(varchar(10),fid) from tb1 where dah=@dah
return stuff(@sql,1,1,'')
end
select dah,dbo.joinstr(dah) fid from tb1 group by dah