select sum(if (a>100) 1 else 0 ) from table
我希望计算 if (a>100) 时返回1  否则返回0 而CASE语句只能使用固定的值不能是用 大于小于号
  sum(case datepart(hour , in_date) when 0 then 1 else 0 end) 
只能
这样

解决方案 »

  1.   

    如果a列是int型的话这样就可以了
    select sum(case (a/100) when 0 then 0 else 1 end) from table 
     
      

  2.   

    如果是计数还可以这样写select count(* ) from table where a> 100
      

  3.   

    可以的
    select sum(case when a> 100 then 1 else 0 end ) from table 
      

  4.   

    declare @s varchar(5)
    set @s='a'
    select case when @s='a' then '等于' else '不等于' endselect case @s when 'a' then '等于' else '不等于' end2种用法