SQL97 验证刷题效果,输出题目真实通过率

牛客刷题记录表`done_questions_record`,为验证重复刷题率,输出题目通过率(哪些题目被通过了,这个人你一共刷题的题目) question_pass_rate 60% 的用户的提交正确率 pass_rate 与每题目平均提交次数 question_per_cnt。 result_info '是否通过,1:通过; 0:不通过',查询返回结果名称和顺序 user_idquestion_pass_ratepass_ratequestion_per_cnt

示例1

输入

drop table if exists `done_questions_record`;
create table `done_questions_record` (
user_id int not null comment '用户id',
question_id int not null comment '题目id',
question_type varchar(24) not null comment '题目类型',
done_time datetime not null comment '提交日期',
result_info int not null comment '是否通过,1:通过; 0:不通过'
);

insert into `done_questions_record` values (101, 1, 'python', '2022-01-01 12:30:21', 0);
insert into `done_questions_record` values (101, 1, 'python', '2022-01-01 12:30:22', 1);
insert into `done_questions_record` values (102, 1, 'python', '2022-01-01 14:30:23', 1);
insert into `done_questions_record` values (101, 2, 'sql', '2022-01-01 16:30:24', 1);
insert into `done_questions_record` values (102, 2, 'sql', '2022-01-02 08:30:25', 1);
insert into `done_questions_record` values (103, 1, 'python', '2022-01-03 10:30:26', 0);
insert into `done_questions_record` values (104, 1, 'python', '2022-01-03 11:30:27', 0);
insert into `done_questions_record` values (103, 2, 'sql', '2022-01-03 19:30:28', 1);
insert into `done_questions_record` values (105, 1, 'python', '2022-01-03 12:30:29', 0);
insert into `done_questions_record` values (103, 3, 'java', '2022-01-03 12:30:30', 0);
insert into `done_questions_record` values (105, 3, 'java', '2022-01-03 12:30:31', 1);
insert into `done_questions_record` values (105, 4, 'js', '2022-01-03 12:30:32', 0);
insert into `done_questions_record` values (104, 5, 'c++', '2022-01-03 12:30:33', 1);
insert into `done_questions_record` values (106, 5, 'c++', '2022-01-03 12:30:34', 0);
insert into `done_questions_record` values (101, 5, 'c++', '2022-01-03 12:30:35', 1);
insert into `done_questions_record` values (106, 5, 'c++', '2022-01-03 12:30:36', 1);
insert into `done_questions_record` values (102, 5, 'c++', '2022-01-03 12:30:37', 1);
insert into `done_questions_record` values (103, 4, 'js', '2022-01-03 12:30:38', 1);
insert into `done_questions_record` values (105, 3, 'java', '2022-01-03 12:30:39', 1);
insert into `done_questions_record` values (103, 2, 'sql', '2022-01-03 12:30:40', 0);
insert into `done_questions_record` values (105, 1, 'python', '2022-01-03 12:30:41', 0);
insert into `done_questions_record` values (105, 1, 'python', '2022-01-03 12:30:42', 1);
insert into `done_questions_record` values (104, 2, 'sql', '2022-01-03 12:30:43', 1);

输出

user_id|question_pass_rate|pass_rate|question_per_cnt
101|1.0000|0.7500|1.3333
102|1.0000|1.0000|1.0000
104|0.6667|0.6667|1.0000
105|0.6667|0.5000|2.0000
106|1.0000|0.5000|2.0000
/*总题目数(去重) count(distinct question_id)
通过题目总数(去重)这个有点难
通过题目总数(不去重) sum(result_info)
提交次数 count(*)
总提交次数 count(*)
*/
select
    user_id,
    count(distinct if(result_info = 1, question_id, null)) / count(distinct question_id) as question_pass_rate,
    sum(result_info) / count(result_info) as pass_rate,
    count(question_id) / count(distinct question_id) as question_per_cnt
from
    done_questions_record
group by
    user_id
having
    question_pass_rate > 0.6;

关键代码分析

1. 题目通过率(去重)

COUNT(DISTINCT IF(result_info = 1, question_id, NULL)) / COUNT(DISTINCT question_id)

计算用户通过的不同题目数,除以该用户做过的不同题目总数。


2. 提交通过率

SUM(result_info) / COUNT(result_info)

计算用户所有提交中,result_info 为 1 的比例。


3. 平均每题提交次数

COUNT(question_id) / COUNT(DISTINCT question_id)

计算用户的总提交次数,除以做过的不同题目数量。

你可能感兴趣的:(MYSQL,数据库)