2016-01-29 123 views
0

我想在90算分數,但這個結束了。如果我的數據是如何計算分數超過90 SQL

100 
45 
90 
100 

它應該顯示3總行

select g.score as score 
     ,count(case when g.score>=90 then 1 else 0 end) over() as passed 
from grades g 

計數,而是它顯示4

回答

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 

或更改COUNTSUM

select g.score as score 
     ,SUM(case when g.score>=90 then 1 else 0 end) over() as passed 
from grades g 
2

只需在SUM編輯COUNTCOUNT計數非空值,所以它甚至計數0; SUM只會'計數'非零值

+0

謝謝大聲笑不,我沒有喝水,我gu it着它 – user1854438