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

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

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

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
孤帆远影碧空尽 文章被收录于专栏

牛客网各类题单题解~

全部评论
请问这里为什么必须要加having的这一句呢?
4 回复 分享
发布于 2022-05-06 10:25
这里一直有个疑问,sql执行顺序是先having再select,为啥having还能用select的字段别名incomplete_cnt 大佬们求指教
3 回复 分享
发布于 2022-03-03 11:39
mark数字马力笔试考到
点赞 回复 分享
发布于 2025-10-28 21:21 湖南
select exam_id ,(count(start_time)-count(submit_time)) incomplete_cnt ,round((count(start_time)-count(submit_time))/count(exam_id),3) incomplete_rate from exam_record group by exam_id 有没有大佬帮我看下我这样写最后为什么提交不通过呢,自测运行是一样的,但是提交报错
点赞 回复 分享
发布于 2025-01-18 15:25 美国
select exam_id,sum(if(score is null, 1, 0)) as incomplete_cnt,count(score)/count(exam_id) from exam_record where submit_time is null group by exam_id having incomplete_cnt >= 1 大佬看一下这个是什么问题呢?
点赞 回复 分享
发布于 2023-02-05 16:36 北京
请问为啥不能用COUNT,之前的题我都是用这个,但是这道题就不行了。另外sum(if(score is null, 1, 0))这里的意思是,逐个成绩判断是否为NULL吗?我还以为是全部加起来再判断
点赞 回复 分享
发布于 2022-05-20 18:24
select exam_id, sum(if(submit_time is null,1,0)) as incomplete_cnt, round(sum(if(submit_time is null,1,0))/count(start_time),3) as incomplete_rate from exam_record where exam_id in(select exam_id from exam_record where start_time is not null and submit_time is null) group by exam_id
点赞 回复 分享
发布于 2022-02-15 09:26

相关推荐

评论
30
1
分享

创作者周榜

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