刚才的错了
改一下
declare @t table(name varchar(20),type int)
insert into @t
select 'a',0
union all select 'b',0
union all select 'fb',1
union all select 'fbf',2
union all select 'fdfs',1
declare @b varchar(1000)
set @b=''
select @b=case @b when ''then '' else @b+',' end +rtrim(name) from @t
select @b name,count_0=sum(case type when 0 then 1 end),
count_1=sum(case type when 1 then 1 end),
count_2=sum(case type when 2 then 1 end)
from @t
declare @t table(name varchar(20),type int)
insert into @t
select 'a',0
union all select 'b',0
union all select 'fb',1
union all select 'fbf',2
union all select 'fdfs',1
declare @b varchar(1000)
set @b=''
select @b=case @b when ''then '' else @b+',' end +rtrim(name) from @t group by name
select @b name,count_0=sum(case type when 0 then 1 end),
count_1=sum(case type when 1 then 1 end),
count_2=sum(case type when 2 then 1 end)
from @t
/*
是不是要的结果是这样的
name count_0 count_1 count_2
a,b,fb,fbf,fdfs 2 2 1