题解 | #未完成试卷数大于1的有效用户#
未完成试卷数大于1的有效用户
https://www.nowcoder.com/practice/46cb7a33f7204f3ba7f6536d2fc04286
为什么出题人在描述中的detail字段的结果展示中和测试用例的结果展示不同?用了三种方式去写,最终结果都是detail字段的顺序问题,他这个描述是真的误导人,
************请注意,在测试用例中,detail字段是按照日期升序排序的。
#统计2021年 #有效用户:完成试卷作答数量 >=1 and 未完成数量 < 5 #且未完成试卷作答数 > 1 #输出用户ID、未完成试卷作答数、完成试卷作答数、 #detail:作答过的试卷tag集合(start_time的日期:tag),集合元素之间用;隔开 #结果顺序: 按照未完成试卷的数量 从大到小排列 SELECT c.uid, MAX(c.failedExam) AS incomplete_cnt, MAX(c.sucessExam) AS complete_cnt, GROUP_CONCAT( DISTINCT DATE_FORMAT(d.start_time, '%Y-%m-%d'), ':', e.tag ORDER BY d.start_time SEPARATOR ";" ) as detail FROM ( SELECT b.uid, b.sucessExam, b.failedExam FROM ( SELECT a.uid, SUM( CASE WHEN a.submit_time IS NOT NULL THEN 1 ELSE 0 END ) AS sucessExam, SUM( CASE WHEN a.submit_time IS NULL THEN 1 ELSE 0 END ) AS failedExam FROM exam_record a WHERE YEAR (a.start_time) = 2021 GROUP BY a.uid ) b WHERE b.sucessExam >= 1 AND b.failedExam < 5 AND b.failedExam > 1 ) c JOIN exam_record d ON c.uid = d.uid and year (d.start_time) = 2021 JOIN examination_info e ON d.exam_id = e.exam_id GROUP BY c.uid ORDER BY complete_cnt