您的位置:首页 > 数据库 > MySQL

统计出每门课程各个成绩等级的学生数量

2016-12-06 10:47 288 查看
学生成绩表sc(id,sid,cid,score),id是sc表的主键,sid是学生编号,cid是课程号,score是对应的分数。分数在90-100之间的等级是A,在80-90之间的等级是B,在70-80之间的是C,在60-70之间的是D,60分之下的是E。现要求统计出每门课程各个成绩等级的学生数量。

SQL语句如下:

select  cid,
sum(case when score>=90 and score<=100 then 1 else 0 end) A,
sum(case when score>=80  and score<90 then 1 else 0 end) B,
sum(case when score>=70  and score<80 then 1 else 0 end) C,
sum(case when score>=60  and score<70 then 1 else 0 end) D,
sum(case when score<60 then 1 else 0 end) E
from sc
group by cid


如sc表的内容如下:



查询的结果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql