列表

详情


SQL129. 月均完成试卷数不小于3的用户爱作答的类别

描述

现有试卷作答记录表exam_record(uid:用户ID, exam_id:试卷ID, start_time:开始作答时间, submit_time:交卷时间,没提交的话为NULL, score:得分),示例数据如下:
id uid exam_id start_time submit_time score
1 1001 9001
2021-07-02 09:01:01
(NULL)
(NULL)
2 1002
9003
2021-09-01 12:01:01
2021-09-01 12:21:01
60
3 1002
9002
2021-09-02 12:01:01
2021-09-02 12:31:01 70
4 1002
9001
2021-09-05 19:01:01
2021-09-05 19:40:01
81
5 1002
9002
2021-07-06 12:01:01
(NULL)
(NULL)
6 1003
9003
2021-09-07 10:01:01
2021-09-07 10:31:01 86
7 1003
9003
2021-09-08 12:01:01
2021-09-08 12:11:01
40
8 1003
9001
2021-09-08 13:01:01
(NULL)
(NULL)
9 1003
9002
2021-09-08 14:01:01
(NULL)
(NULL)
10 1003
9003
2021-09-08 15:01:01
(NULL)
(NULL)
11 1005
9001
2021-09-01 12:01:01
2021-09-01 12:31:01
88
12 1005
9002
2021-09-01 12:01:01
2021-09-01 12:31:01
88
13 1005
9002
2021-09-02 12:11:01
2021-09-02 12:31:01
89

试卷信息表examination_info(exam_id:试卷ID, tag:试卷类别, difficulty:试卷难度, duration:考试时长, release_time:发布时间),示例数据如下:
id exam_id tag difficulty duration
release_time
1 9001 SQL hard 60 2020-01-01 10:00:00
2 9002 C++ easy
60
2020-02-01 10:00:00
3 9003
算法
medium
80
2020-08-02 10:00:00


请从表中统计出 “当月均完成试卷数”不小于3的用户们爱作答的类别及作答次数,按次数降序输出,示例输出如下:
tag tag_cnt
C++ 4
SQL 2
算法 1

解释:用户1002和1005在2021年09月的完成试卷数目均为3,其他用户均小于3;然后用户1002和1005作答过的试卷tag分布结果按作答次数降序排序依次为C++、SQL、算法。

示例1

输入:

drop table if exists examination_info,exam_record;
CREATE TABLE examination_info (
    id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
    exam_id int UNIQUE NOT NULL COMMENT '试卷ID',
    tag varchar(32) COMMENT '类别标签',
    difficulty varchar(8) COMMENT '难度',
    duration int NOT NULL COMMENT '时长',
    release_time datetime COMMENT '发布时间'
)CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE TABLE exam_record (
    id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
    uid int NOT NULL COMMENT '用户ID',
    exam_id int NOT NULL COMMENT '试卷ID',
    start_time datetime NOT NULL COMMENT '开始时间',
    submit_time datetime COMMENT '提交时间',
    score tinyint COMMENT '得分'
)CHARACTER SET utf8 COLLATE utf8_general_ci;

INSERT INTO examination_info(exam_id,tag,difficulty,duration,release_time) VALUES
  (9001, 'SQL', 'hard', 60, '2020-01-01 10:00:00'),
  (9002, 'C++', 'easy', 60, '2020-02-01 10:00:00'),
  (9003, '算法', 'medium', 80, '2020-08-02 10:00:00');

INSERT INTO exam_record(uid,exam_id,start_time,submit_time,score) VALUES
(1001, 9001, '2021-07-02 09:01:01', null, null),
(1002, 9003, '2021-09-01 12:01:01', '2021-09-01 12:21:01', 60),
(1002, 9002, '2021-09-02 12:01:01', '2021-09-02 12:31:01', 70),
(1002, 9001, '2021-09-05 19:01:01', '2021-09-05 19:40:01', 81),
(1002, 9002, '2021-07-06 12:01:01', null, null),
(1003, 9003, '2021-09-07 10:01:01', '2021-09-07 10:31:01', 86),
(1003, 9003, '2021-09-08 12:01:01', '2021-09-08 12:11:01', 40),
(1003, 9001, '2021-09-08 13:01:01', null, null),
(1003, 9002, '2021-09-08 14:01:01', null, null),
(1003, 9003, '2021-09-08 15:01:01', null, null),
(1005, 9001, '2021-09-01 12:01:01', '2021-09-01 12:31:01', 88),
(1005, 9002, '2021-09-01 12:01:01', '2021-09-01 12:31:01', 88),
(1005, 9002, '2021-09-02 12:11:01', '2021-09-02 12:31:01', 89);

输出:

C++|4
SQL|2
算法|1

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Mysql 解法, 执行用时: 36ms, 内存消耗: 6384KB, 提交时间: 2022-02-08

select tag, count(start_time) as tag_cnt
from exam_record er inner join examination_info ei
on er.exam_id = ei.exam_id
where uid in 
(select uid
from exam_record er 
group by uid, month(start_time)
having count(submit_time) >= 3)
group by tag
order by tag_cnt desc

Mysql 解法, 执行用时: 36ms, 内存消耗: 6412KB, 提交时间: 2021-12-26

with t1 as (
    select er.*,COUNT(submit_time) over(partition by uid,DATE_FORMAT(submit_time,"%y-%m")) as sub_times from exam_record er 
)

select tag,count(*) times from t1
left join examination_info ei on ei.exam_id = t1.exam_id
where t1.uid in (
    select distinct uid from t1 
    where sub_times>=3
)
group by tag
order by times desc

Mysql 解法, 执行用时: 36ms, 内存消耗: 6416KB, 提交时间: 2022-01-24

select tag, count(exam_record.exam_id) tag_cnt
from exam_record
inner join examination_info
on exam_record.exam_id = examination_info.exam_id
where uid in (
    select uid 
    from exam_record
    inner join examination_info
    on exam_record.exam_id = examination_info.exam_id
    where submit_time is not null and month(submit_time) = 9
    group by uid
    having count(exam_record.exam_id) >= 3
)
group by tag
order by tag_cnt desc;

Mysql 解法, 执行用时: 36ms, 内存消耗: 6500KB, 提交时间: 2022-01-24

select tag,count(a.id) as ss from exam_record a
join examination_info b
on a.exam_id=b.exam_id
where a.uid in
(SELECT
	uid
FROM
	exam_record 
GROUP BY
	uid,
	date_format( submit_time, '%Y%m' ) 
HAVING
	sum(if( submit_time IS NOT NULL, 1, 0 )) >=3)  GROUP BY tag order by ss desc

Mysql 解法, 执行用时: 36ms, 内存消耗: 6540KB, 提交时间: 2021-12-14

select 
    ei.tag,
    count(er.exam_id) as tag_cnt 
from 
(select 
uid
from exam_record 
where submit_time is not null 
group by uid
having (count(exam_id)/count(distinct substr(submit_time,1,7)))>=3
) a 
left join exam_record er on a.uid=er.uid 
left join examination_info ei on er.exam_id=ei.exam_id 
group by ei.tag 
order by count(er.exam_id) desc