我想将得分超过90,但最终计算总行数
select g.score as score
,count(case when g.score>=90 then 1 else 0 end) over () as passed
from grades g
如果我的数据是
100
45
90
100
它应该显示3,但它显示4
答案 0 :(得分:2)
您要么删除ELSE
部分,那么默认值为NULL而COUNT
不计算NULL
select g.score as score
,count(case when g.score>=90 then 1 end) over () as passed
from grades g
或更改COUNT
SUM
select g.score as score
,SUM(case when g.score>=90 then 1 else 0 end) over () as passed
from grades g
答案 1 :(得分:2)
只需在COUNT
中修改SUM
即可。 COUNT
计数不为空值,因此它甚至计为0; SUM
只会'计算'非零值