描述
题目:现在运营想要查看用户在某天刷题后第二天还会再来刷题的平均概率。请你取出相应数据。
示例:question_practice_detail
id | device_id | quest_id | result | date |
---|---|---|---|---|
1 | 2138 | 111 | wrong | 2021-05-03 |
2 | 3214 | 112 | wrong | 2021-05-09 |
3 | 3214 | 113 | wrong | 2021-06-15 |
4 | 6543 | 111 | right | 2021-08-13 |
5 | 2315 | 115 | right | 2021-08-13 |
6 | 2315 | 116 | right | 2021-08-14 |
7 | 2315 | 117 | wrong | 2021-08-15 |
…… |
根据示例,你的查询应返回以下结果:
avg_ret |
---|
0.3000 |
示例1
输入:
drop table if exists `user_profile`;
drop table if exists `question_practice_detail`;
drop table if exists `question_detail`;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`gpa` float,
`active_days_within_30` int ,
`question_cnt` int ,
`answer_cnt` int
);
CREATE TABLE `question_practice_detail` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`question_id`int NOT NULL,
`result` varchar(32) NOT NULL,
`date` date NOT NULL
);
CREATE TABLE `question_detail` (
`id` int NOT NULL,
`question_id`int NOT NULL,
`difficult_level` varchar(32) NOT NULL
);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25);
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30);
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2);
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70);
INSERT INTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13);
INSERT INTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);
INSERT INTO question_practice_detail VALUES(1,2138,111,'wrong','2021-05-03');
INSERT INTO question_practice_detail VALUES(2,3214,112,'wrong','2021-05-09');
INSERT INTO question_practice_detail VALUES(3,3214,113,'wrong','2021-06-15');
INSERT INTO question_practice_detail VALUES(4,6543,111,'right','2021-08-13');
INSERT INTO question_practice_detail VALUES(5,2315,115,'right','2021-08-13');
INSERT INTO question_practice_detail VALUES(6,2315,116,'right','2021-08-14');
INSERT INTO question_practice_detail VALUES(7,2315,117,'wrong','2021-08-15');
INSERT INTO question_practice_detail VALUES(8,3214,112,'wrong','2021-05-09');
INSERT INTO question_practice_detail VALUES(9,3214,113,'wrong','2021-08-15');
INSERT INTO question_practice_detail VALUES(10,6543,111,'right','2021-08-13');
INSERT INTO question_practice_detail VALUES(11,2315,115,'right','2021-08-13');
INSERT INTO question_practice_detail VALUES(12,2315,116,'right','2021-08-14');
INSERT INTO question_practice_detail VALUES(13,2315,117,'wrong','2021-08-15');
INSERT INTO question_practice_detail VALUES(14,3214,112,'wrong','2021-08-16');
INSERT INTO question_practice_detail VALUES(15,3214,113,'wrong','2021-08-18');
INSERT INTO question_practice_detail VALUES(16,6543,111,'right','2021-08-13');
INSERT INTO question_detail VALUES(1,111,'hard');
INSERT INTO question_detail VALUES(2,112,'medium');
INSERT INTO question_detail VALUES(3,113,'easy');
INSERT INTO question_detail VALUES(4,115,'easy');
INSERT INTO question_detail VALUES(5,116,'medium');
INSERT INTO question_detail VALUES(6,117,'easy');
复制
输出:
0.3000
解题思路
根据题意我们可以得出如下几个条件
-
我们要知道所有来刷题的用户统计,而且要去重 count(date1)
count(date1) date1代表一天的用户
-
我们还需要在第一天来基础上第二天来的用户 count(date2)
count(date2)
还是假设一张已经获得所有结果的表t我们可以通过该表t一次查询出我们所需要的内容,分母是用户第一天答题统计,分子是用户第二天答题统计
可以得到如下表达式select count(date1)/ count(date2) from t;
接下来我们要开始组装表t,题中只给了一张表,而我们却要获取两种数据,所以表段需要内联一下生成一个新的表。那么该表需要存储什么信息?
依照题意分析最好存储的是 : 用户第一天来的日期,用户第二天来的日期和用于区分不同用户的id。而且不能有重复数据,所以用户id还要做一个去重
所以可得到如下语句生成该表段,为表t表段
select distinct device_id,qf.date,qd.date
from question_practice_detail as qf left join
question_practice_detail as qd
on qf.device_id=qd.device_id and date_add(qf.date,interval 1 day)=qd.date
所以更新后语句为
select
count(date1)/ count(date2)
from (select distinct device_id,qf.date,qd.date
from question_practice_detail as qf left join
question_practice_detail as qd
on qf.device_id=qd.device_id and date_add(qf.date,interval 1 day)=qd.date) as tt;
之后我们需要对date与date2做一次修改,
count(case
when tt.date2 is not null then tt.date2
end) / count(tt.date1)
则最后得出题解表达式
select
count(case
when tt.date2 is not null then tt.date2
end) / count(tt.date1)
from
(select distinct q1.device_id,q1.date date1,q2.date date2
from
question_practice_detail as q1 left join question_practice_detail as q2
on q1.device_id=q2.device_id and date_add(q1.date, INTERVAL 1 day)=q2.date ) as tt;