Mysql刷题题解_多表联查_取出用户在某天刷题后再来刷题的平均概率

描述

题目:现在运营想要查看用户在某天刷题后第二天还会再来刷题的平均概率。请你取出相应数据。

示例:question_practice_detail

iddevice_idquest_idresultdate
12138111wrong2021-05-03
23214112wrong2021-05-09
33214113wrong2021-06-15
46543111right2021-08-13
52315115right2021-08-13
62315116right2021-08-14
72315117wrong2021-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

解题思路

根据题意我们可以得出如下几个条件

  1. 我们要知道所有来刷题的用户统计,而且要去重 count(date1)

     count(date1) date1代表一天的用户
    
  2. 我们还需要在第一天来基础上第二天来的用户 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;

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值