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

题目主要信息:

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

问题拆分:

  • 要统计每份试卷的未完成情况,因此要以exam_id分组。知识点:group by
  • 试卷ID直接获取。
  • 未完成试卷数,统计每组得分为null的总数。sum(if(score is null, 1, 0)) as incomplete_cnt 知识点:sum()、if
  • 根据每组未完成试卷数和每组开始做题的时间数求未完成率。round(sum(if(score is null, 1, 0)) / count(start_time), 3) as incomplete_rate 知识点:round、sum、if、count
  • 最后要过滤掉未完成数不足1的分组。知识点:having

代码:

select exam_id,
       sum(if(score is null, 1, 0)) as incomplete_cnt,
       round(sum(if(score is null, 1, 0)) / count(start_time), 3) as incomplete_rate
from exam_record
group by exam_id
having incomplete_cnt >= 1