[SQL]SQL类似统计功能的sql文

时间:2021-05-15 15:26:53
declare @t table(name varchar(),type int)
insert into @t
select 'a',
union all select 'b',
union all select 'fb',
union all select 'fbf',
union all select 'fdfs', if object_id('test1') is not null
drop table test1
create table test1(
name varchar(),
type int)
insert into test1
select 'a',
union all select 'b',
union all select 'fb',
union all select 'fbf',
union all select 'fdfs', select * from test1 create function ss()
returns varchar()
as
begin
declare @sql varchar()
set @sql=''
select @sql=@sql+','+ name from test1
set @sql=stuff(@sql,,,' ')
return @sql
end select dbo.ss(),count_0=(select count() from test1 where type=),
count_1=(select count() from test1 where type=),
count_2=(select count() from test1 where type=)
/*
我想要的结果是这样的
name count_0 count_1 count_2
a,b,fb,fbf,fdfs 2 2 1 */