当前位置:网站首页>SQL29 Calculate the average next day retention rate of users
SQL29 Calculate the average next day retention rate of users
2022-08-01 22:07:00 【java factory manager】
SQL29 计算用户的平均次日留存率
携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第4天,点击查看活动详情
1、题目
现在运营想要查看用户在某天刷题后第二天还会再来刷题的平均概率.请你取出相应数据.
示例: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
复制代码
2、思路🧠
问题分解:
- 限定条件
- 平均概率
解决方法:
- 表头重命名:as
- 去重:按照devece_id,date去重,因为一个人一天可能来多次
- 子查询必须全部有重命名,The joint table query needs to specify the table name.
3、代码
commit AC
select count(date2) / count(date1) as avg_ret
from (
select
distinct qpd.device_id,
qpd.date as date1,
uniq_id_date.date as date2
from question_practice_detail as qpd
left join(
select distinct device_id, date
from question_practice_detail
) as uniq_id_date
on qpd.device_id=uniq_id_date.device_id
and date_add(qpd.date, interval 1 day)=uniq_id_date.date
) as id_last_next_date
复制代码
4、总结
该题目的对SQL的语法及基础知识,学会使用IF、CASE等的条件查询也要会使用,像内连接、外连接、左连接、右连接等都要有相关的了解,其次当你编写了大量的SQL之后,就要学会进行SQL的优化,这对于数据查询的时间会有大幅度的降低.
️来自专栏《Mysql每日一题》欢迎订阅️
厂长写博客目的初衷很简单,希望大家在学习的过程中少走弯路,多学一些东西,对自己有帮助的留下你的赞赞或者关注都是对我最大的支持,你的关注和点赞给厂长每天更文的动力.
对文章其中一部分不理解,都可以评论区回复我,我们来一起讨论,共同学习,一起进步!
边栏推荐
- 如何理解 new (...args: any[]) => any
- SAP ABAP OData 服务如何支持删除(Delete)操作试读版
- 2022 edition of MySQL tutorial, top collection good, take your time
- JS prototype hasOwnProperty in 加方法 原型终点 继承 重写父类方法
- NgRx Selector 的 Memoization 特性学习笔记
- MySQL related knowledge
- _ _ determinant of a matrix is higher algebra eigenvalue of the product, the characteristic value of matrix trace is combined
- Small program -- subcontracting
- Advanced Algebra_Proof_The algebraic multiplicity of any eigenvalue of a matrix is greater than or equal to its geometric multiplicity
- Postman 批量测试接口详细教程
猜你喜欢
求解多元多次方程解的个数
19 Lectures on Disassembly of Multi-merchant Mall System Functions - Invoice Management on the Platform
论文解读(GSAT)《Interpretable and Generalizable Graph Learning via Stochastic Attention Mechanism》
Kubernetes Scheduler全解析
小程序毕设作品之微信美食菜谱小程序毕业设计成品(7)中期检查报告
(Translation) How the contrasting color of the button guides the user's actions
_ _ determinant of a matrix is higher algebra eigenvalue of the product, the characteristic value of matrix trace is combined
联邦学习入门
Based on php online learning platform management system acquisition (php graduation design)
How to add a game character to a UE4 scene
随机推荐
365 days challenge LeetCode1000 questions - Day 046 Generate a string with odd number of each character + add two numbers + valid parentheses
今日睡眠质量记录74分
【建议收藏】ヾ(^▽^*)))全网最全输入输出格式符整理
xctf attack and defense world web master advanced area webshell
APP专项测试:流量测试
No more rolls!After joining ByteDance for a week, he ran decisively.
User Experience | How to Measure User Experience?
使用分类权重解决数据不平衡的问题
NgRx Store createSelector 的单步调试和源代码分析
熟悉的朋友
一种灵活的智能合约协作方式
解决 win10 下 ISE14.7的 iMPACT 崩溃问题 - FPGA 笔记
19 Lectures on Disassembly of Multi-merchant Mall System Functions - Invoice Management on the Platform
联邦学习入门
【移动Web】移动端适配
Delicious this year
还在纠结报表工具的选型么?来看看这个
seaborn笔记:可视化统计关系(散点图、折线图)
SOM网络1:原理讲解
将vim与系统剪贴板的交互使用