题目

image.png

image.png

建表语句

  1. drop table if exists examination_info;
  2. CREATE TABLE examination_info (
  3. id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
  4. exam_id int UNIQUE NOT NULL COMMENT '试卷ID',
  5. tag varchar(32) COMMENT '类别标签',
  6. difficulty varchar(8) COMMENT '难度',
  7. duration int NOT NULL COMMENT '时长',
  8. release_time datetime COMMENT '发布时间'
  9. )CHARACTER SET utf8 COLLATE utf8_general_ci;
  10. drop table if exists exam_record;
  11. CREATE TABLE exam_record (
  12. id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
  13. uid int NOT NULL COMMENT '用户ID',
  14. exam_id int NOT NULL COMMENT '试卷ID',
  15. start_time datetime NOT NULL COMMENT '开始时间',
  16. submit_time datetime COMMENT '提交时间',
  17. score tinyint COMMENT '得分'
  18. )CHARACTER SET utf8 COLLATE utf8_general_ci;
  19. INSERT INTO examination_info(exam_id,tag,difficulty,duration,release_time) VALUES
  20. (9001, 'SQL', 'hard', 60, '2020-01-01 10:00:00'),
  21. (9002, 'SQL', 'easy', 60, '2020-02-01 10:00:00'),
  22. (9003, '算法', 'medium', 80, '2020-08-02 10:00:00');
  23. INSERT INTO exam_record(uid,exam_id,start_time,submit_time,score) VALUES
  24. (1001, 9001, '2021-07-02 09:01:01', '2021-07-02 09:21:01', 80),
  25. (1002, 9001, '2021-09-05 19:01:01', '2021-09-05 19:40:01', 81),
  26. (1002, 9002, '2021-09-02 12:01:01', null, null),
  27. (1002, 9003, '2021-09-01 12:01:01', null, null),
  28. (1002, 9001, '2021-07-02 19:01:01', '2021-07-02 19:30:01', 82),
  29. (1002, 9002, '2021-07-05 18:01:01', '2021-07-05 18:59:02', 90),
  30. (1003, 9002, '2021-07-06 12:01:01', null, null),
  31. (1003, 9003, '2021-09-07 10:01:01', '2021-09-07 10:31:01', 86),
  32. (1004, 9003, '2021-09-06 12:01:01', null, null),
  33. (1002, 9003, '2021-09-01 12:01:01', '2021-09-01 12:31:01', 81),
  34. (1005, 9001, '2021-09-01 12:01:01', '2021-09-01 12:31:01', 88),
  35. (1005, 9002, '2021-09-01 12:01:01', '2021-09-01 12:31:01', 88),
  36. (1006, 9002, '2021-09-02 12:11:01', '2021-09-02 12:31:01', 89);

解题思路

题意提炼
统计2021年每个【未完成试卷作答数大于1】的【有效用户】的数据(有效用户指完成试卷作答数至少为1且未完成数小于5),输出用户ID、未完成试卷作答数、完成试卷作答数、作答过的试卷tag集合,按未完成试卷数量由多到少排序

问题分解

  • 关联作答记录和试卷信息:left join examination_info on using(exam_id);(题中exam_record中的exam_idexamination_info均存在,所以用left joininner join效果一样)
  • 筛选2021年的记录:where year(start_time)=2021
  • 获取各用户的tag,start_time及未完成标记和已完成标记,如果该作答记录交卷了则已完成标记为1,未完成标记为0,否则相反:if(submit_time is null, 1, null) as incomplete
  • 按用户分组:group by uid
  • 统计未完成试卷作答数和已完成试卷作答数:count(incomplete) as incomplete_cnt
  • 统计作答过的tag集合:
    • 对于每条作答tag,用:连接日期和tag:concat_ws(':', date(start_time), tag)
    • 对于一个人(组内)的多条作答,用;连接去重后的作答记录:group_concat(distinct concat_ws(':', date(start_time), tag) SEPARATOR ';')
  • 筛选未完成试卷作答数大于1的有效用户:having complete_cnt >= 1 and incomplete_cnt BETWEEN 2 and 4
    • 完成试卷作答数至少为1:complete_cnt >= 1
    • 未完成数小于5:incomplete_cnt < 5
    • 未完成试卷作答数大于1:incomplete_cnt > 1

SQL代码

SELECT uid, count(incomplete) as incomplete_cnt,
    count(complete) as complete_cnt,
    group_concat(distinct concat_ws(':', date(start_time), tag) SEPARATOR ';') as detail
from (
    SELECT uid, tag, start_time,
        if(submit_time is null, 1, null) as incomplete,
        if(submit_time is null, null, 1) as complete
    from exam_record
    left join examination_info using(exam_id)
    where year(start_time)=2021
) as exam_complete_rec
group by uid
having complete_cnt >= 1 and incomplete_cnt BETWEEN 2 and 4
order by incomplete_cnt DESC