题解 | #统计有未完成状态的试卷的未完成数和未完成率#
统计有未完成状态的试卷的未完成数和未完成率
http://www.nowcoder.com/practice/69fc2b1df4144c0991e4b8280d8aba27
题目分析
1、统计有未完成状态的试卷——子查询+distinct,在子查询里根据submit_time或score为null筛选出exam_id,可以加上去重;
2、统计其未完成数incomplete_cnt——count(start_time)-count(score);
3、未完成率incomplete_rate——round(1-count(score)/count(start_time),3),注意保留3位小数;
答案参考
SELECT exam_id,(count(start_time)-count(score)) as incomplete_cnt, round(1-count(score)/count(start_time),3)as complete_rate FROM exam_record WHERE exam_id in (SELECT distinct exam_id FROM exam_record WHERE score is null) GROUP BY exam_id;