明确题意:

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


问题分解:

  • 统计每次作答是否未完成,生成子表 t_exam_record_complete:
    • 计算完成状态:IF(score IS NULL, 1, 0) as is_incomplete
  • 按试卷分组:GROUP BY exam_id
  • 计算每张试卷的未完成数和未完成率:
    • 未完成数:SUM(is_incomplete) as incomplete_cnt
    • 未完成率:AVG(is_incomplete) as incomplete_rate
    • 保留3位小数:ROUND(x, 3)
  • 筛选有未完成的分组(试卷):HAVING incomplete_cnt > 0

细节问题:

  • 表头重命名:as

完整代码:

SELECT exam_id, SUM(is_incomplete) as incomplete_cnt,
    round(AVG(is_incomplete), 3) as incomplete_rate
FROM (
    SELECT exam_id, IF(score IS NULL, 1, 0) as is_incomplete
    FROM exam_record
) as t_exam_record_complete
GROUP BY exam_id
HAVING incomplete_cnt > 0