SQL VQ14 统计每天刷题数超过5的user_id以及刷题数
- 数据
- 需求
- 解决
题目 : https://www.nowcoder.com/practice/852a14b888c448f1a26f0e2e20f668a7?tpId=341
数据
drop table if exists questions_pass_record_detail;CREATE TABLE `questions_pass_record_detail` (
`user_id` int NOT NULL,
`question_type` varchar(32) NOT NULL,
`device` varchar(14) NOT NULL,
`pass_count` int NOT NULL,
`date` date NOT NULL
);INSERT INTO questions_pass_record_detail VALUES(101, 'java', 'app', 2, '2020-03-01');
INSERT INTO questions_pass_record_detail VALUES(102, 'sql', 'pc', 15,'2021-07-07');
INSERT INTO questions_pass_record_detail VALUES(102, 'python', 'pc', 9, '2021-04-09');
INSERT INTO questions_pass_record_detail VALUES(104, 'python', 'app', 3,'2022-03-17');
INSERT INTO questions_pass_record_detail VALUES(105, 'sql', 'pc', 60, '2016-08-15');
INSERT INTO questions_pass_record_detail VALUES(204, 'sql', 'pc', 20, '2019-05-15');
需求
统计每天刷题数超过 5 的 user_id , 刷题数
查询结果 :
date|user_id|total_pass_count
2021-07-07|102|15
2021-04-09|102|9
解决
注意点 :
- 查询顺序 : from -> where -> group (含聚合) -> having -> order -> select
select `date`,user_id,sum(pass_count) as total_pass_count
from questions_pass_record_detail
group by `date`, user_id
having total_pass_count > 5;