sql题目,统计每个班级考试成绩的合格率,优秀率

一张表存储了学生的成绩,班级号等信息,统计各个班的合格率(>=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值的特点。

以上没有计算百分比,就是一个简单的除法。


  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值