题解 | #SQL15 统计作答次数#

统计作答次数

http://www.nowcoder.com/practice/45a87639110841b6950ef6a12d20175f

明确题意:

统计出总作答次数total_pv、试卷已完成作答数complete_pv、已完成的试卷数complete_exam_cnt


问题分解:

  • 总作答次数:count(exam_id) as total_pv;
  • 试卷已完成作答数,count(A)会忽略A的值为null的行:count(submit_time) as complete_pv;
  • 已完成的试卷数,已完成时才计数用if判断,试卷可能被完成多次,需要去重用distinct:count(distinct if(submit_time is not null, exam_id, null)) as complete_exam_cnt

完整代码:

select
    count(exam_id) as total_pv,
    count(submit_time) as complete_pv,
    count(distinct if(submit_time is not null, exam_id, null)) as complete_exam_cnt
from exam_record
SQL进阶 文章被收录于专栏

SQL进阶step by step

全部评论
最后一个错了
1 回复 分享
发布于 2023-01-08 16:05 江西
感觉最后一个这样算出来是试卷种类数
1 回复 分享
发布于 2022-11-17 16:28 陕西
那我用 count(distinct exam_id)可以吗?
1 回复 分享
发布于 2022-03-24 22:10
请问complete_exam_cnt计数时会算上NULL嘛?
点赞 回复 分享
发布于 01-04 11:45 湖北
优雅
点赞 回复 分享
发布于 2023-09-01 15:09 福建

相关推荐

不愿透露姓名的神秘牛友
05-01 13:13
ecece:这么明目张胆虚报就业率啊
点赞 评论 收藏
分享
评论
148
13
分享

创作者周榜

更多
牛客网
牛客企业服务