一张表存储了学生的成绩,班级号等信息,统计各个班的合格率(>=60)、优秀率(>=80)。要考虑到有的班级没有人通过或者合格的情况。
建表:
create table score(s_id number(10),num number(3),class_id number(10));
插入测试数据:
insert into score values(1,90,1001);
insert into score values(2,60,1001);
insert into score values(3,40,1001);
insert into score values(7,50,1002);
insert into score values(4,60,1002);
insert into score values(5,80,1003);
insert into score values(6,20,1003);
第一种方式:
select (select count( *) from score s1 where num>=60 and class_id=s.class_id), (select count( *) from score s1 where num>=80 and class_id=s.class_id),count( *) from score s group by class_id;
这种方式部分数据库不支持:以结果集作为查询列。
第二种方式
select count(case when num>=60 then 1 else null end),count(case when num>=80 then 1 else null end),count(*) from score group by class_id;
利用count忽略null值的特点。
以上没有计算百分比,就是一个简单的除法。
sql题目,统计每个班级考试成绩的合格率,优秀率
最新推荐文章于 2024-05-17 17:01:47 发布