题解 | #统计有未完成状态的试卷的未完成数和未完成率#

统计有未完成状态的试卷的未完成数和未完成率

https://www.nowcoder.com/practice/69fc2b1df4144c0991e4b8280d8aba27

一、知识点

1.子查询tb

目标:计算出未完成的试卷总数

  1. exam_id进行分组
  2. 使用sumcase when分条件计算总数

sum 和case when 是记数的好搭档,它和sum if的区别就是,它支持更多的查询条件

2.最外层select

根据题目要求即可

注意:记得distinct exam_id,不然就会求出一堆exam_id对应的相同的结果!

select
    distinct exam_id,
    incomplete_cnt,
    round((incomplete_cnt / total),3) as complete_rate
from
    exam_record
    inner join (
        select
            distinct exam_id,
            sum(
                case
                    when start_time is not null
                    and submit_time is null then 1
                    else null
                end
            ) as incomplete_cnt,
            count(exam_id) as total
        from
            exam_record
        group by
            exam_id
    ) as tb using (exam_id)
where submit_time is null
order by exam_id
全部评论

相关推荐

点赞 评论 收藏
分享
11-04 10:30
已编辑
门头沟学院 研发工程师
开心小狗🐶:“直接说答案”
点赞 评论 收藏
分享
11-11 22:08
佛山大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务