一条sql语句,高手挑战一下吧
select ____ as 非零类型个数from table1
declare @table1 table(c1 varchar(1),c2 int)insert into @table1select 'A', 2 union allselect 'A', 4 union allselect 'B', 0 union allselect 'B', 5 union allselect 'C', 0select sum(cnt) 非零类型个数 from( select case when max(c2)>0 then 1 else 0 end cnt from @table1 group by c1)t/*非零类型个数-----------2*/
[解决办法]
SUM(CASE WHEN EXISTS(SELECT 1 FROM TABLE1 WHERE 数值<>0) THEN 1 ELSE 0 END)
[解决办法]
declare @table1 table([类型] varchar(1),[数值] int)insert @table1select 'A',2 union allselect 'A',4 union allselect 'B',0 union allselect 'B',5 union allselect 'C',0select top 1 sum(1) as 非零类型个数from @table1 t group by [类型] having(sum([数值])<>0)/*非零类型个数-----------2*/
[解决办法]
declare @table1 table(c1 varchar(1),c2 int)insert into @table1select 'A', 2 union allselect 'A', 4 union allselect 'A', 4 union allselect 'B', 0 union allselect 'B', 5 union allselect 'C', 0select count(distinct case when c2 > 0 then c1 else null end ) as 非零类型个数from @table1