日期:2014-05-17 浏览次数:20430 次
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 */
------解决方案--------------------
SUM(CASE WHEN EXISTS(SELECT 1 FROM TABLE1 WHERE 数值<>0) THEN 1 ELSE 0 END)
------解决方案--------------------
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