题解 | #统计作答次数#
统计作答次数
https://www.nowcoder.com/practice/45a87639110841b6950ef6a12d20175f
# 1.单表查询,关键在于对题意的理解 # total_pv计算:count(start_time) # complete_pv计算:count(submit_time) # 从submit_time is not null 的试卷中计算:complete_exam_cnt: distinct count(exam_id) 是错的,因为这样会把没有完成的试卷也算上,所以需要用到case when进行筛选。 # 还有一个方法,就是加一个子查询来筛选出submit_time is not null 的exam_id select count(start_time) as total_pv, count(submit_time) as complete_pv, count(distinct case when submit_time is not null then exam_id else null end) as complete_exam_cnt from exam_record