看不懂这个SQL语句啊

要求是
统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]
sc是成绩表,course是课程表
下面是语句
select sc.cid as 课程ID,course.name as 课程名称
,sum(case when score between 85 and 100 then 1 else 0 end)as [100-85]
,sum(case when score between 70 and 85 then 1 else 0 end) as [70-85]
,sum(case when score between 60 and 70 then 1 else 0 end) as [60-70]
,sum(case when score < 60 then 1 else 0 end)as [<60]
from sc,course
where sc.cid=course.cid
group by sc.cid,course.name

为什么用的是sum而不是count?

用sum 是因为里面的case 子句把合条件的变成的,不合条件的变成0,所以才用SUM进行合计,而不是用COUNT进行行数统计
case when score between 85 and 100 then 1 else 0 end
温馨提示:答案为网友推荐,仅供参考
相似回答