sqlserver sum 和count在關於進行統計時的區別


    sum是對內容的數量進行相加,count 對表行數 對象進行統計

 

在使用 case 時,如

 

select subject,
count(case when score>80 then score else null end) 優,
count(case when score<80 and score>59 then score else null end) 優,
count(case when score<60 then score else null end) 優
from stuscore group by [subject]

select * from stuscore

 

 

select subject,
sum(case when score>80 then score else 1 end) 優,
sum(case when score<80 and score>59 then score else 1end) 優,
sum(case when score<60 then score else 1end) 優
from stuscore group by [subject]

select * from stuscore

 

 

這個時候 對sum 進行返回的是1,而對count 返回的是一列(null),或者說是一個行對象進行對數量的統計


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM