一条sql语句,高手挑战一下吧
select ____ as 非零类型个数
from table1
declare @table1 table(c1 varchar(1),c2 int)
insert into @table1
select 'A', 2 union all
select 'A', 4 union all
select 'B', 0 union all
select 'B', 5 union all
select 'C', 0
select sum(cnt) 非零类型个数 from
(
select case when max(c2)>0 then 1 else 0 end cnt
from @table1
group by c1
)t
/*
非零类型个数
-----------
2
*/
declare @table1 table([类型] varchar(1),[数值] int)
insert @table1
select 'A',2 union all
select 'A',4 union all
select 'B',0 union all
select 'B',5 union all
select 'C',0
select top 1 sum(1) as 非零类型个数
from @table1 t group by [类型] having(sum([数值])<>0)
/*
非零类型个数
-----------
2
*/
declare @table1 table(c1 varchar(1),c2 int)
insert into @table1
select 'A', 2 union all
select 'A', 4 union all
select 'A', 4 union all
select 'B', 0 union all
select 'B', 5 union all
select 'C', 0
select count(distinct case when c2 > 0 then c1 else null end ) as 非零类型个数
from @table1