当前位置:网站首页>Offset function and windowing function
Offset function and windowing function
2022-07-04 19:57:00 【Lime Town in the sun】
Case study
Calculate the average next day retention rate of users
Create table statement
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,' Peking University, ',3.4,7,2,12);
INSERT INTO user_profile VALUES(2,3214,'male',null,' Fudan University ',4.0,15,5,25);
INSERT INTO user_profile VALUES(3,6543,'female',20,' Peking University, ',3.2,12,3,30);
INSERT INTO user_profile VALUES(4,2315,'female',23,' Zhejiang University ',3.6,5,1,2);
INSERT INTO user_profile VALUES(5,5432,'male',25,' Shandong University ',3.8,20,15,70);
INSERT INTO user_profile VALUES(6,2131,'male',28,' Shandong University ',3.3,15,7,13);
INSERT INTO user_profile VALUES(7,4321,'male',28,' Fudan University ',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');
select avg(if(DATEDIFF(date2,date1)=1,1,0)) as avg_ret from
(select date as date1,lead(date,1) over(partition by device_id order by date) as date2 from
(select DISTINCT device_id,date from question_practice_detail) as unique_date) as aa
A few problems to pay attention to :
1、if function
2、lead function ,lead( Column or function , How many lines in the future do not fill in the default 1, The default value defaults to null)
3、distinct Act on multiple columns , In fact, according to multiple columns to remove the heavy ;
4、 Subqueries use aliases
边栏推荐
- kotlin 基本使用
- 1003 Emergency(25 分)(PAT甲级)
- English grammar_ Noun - use
- . Net ORM framework hisql practice - Chapter 2 - using hisql to realize menu management (add, delete, modify and check)
- 明明的随机数
- Siemens HMI download prompts lack of panel image solution
- C# 使用StopWatch测量程序运行时间
- Several methods of online database migration
- HMM hidden Markov model and code implementation
- 上线首月,这家露营地游客好评率高达99.9%!他是怎么做到的?
猜你喜欢
黑马程序员-软件测试--09阶段2-linux和数据库-31-43修改文件权限字母发的说明,-查找链接修改文件,查找文件命令,链接文件,压缩解压方式,vi编辑器基本使用,
【问题】druid报异常sql injection violation, part alway true condition not allow 解决方案
Opencv functions and methods related to binary threshold processing are summarized for comparison and use
ACM组合计数入门
Detailed explanation of the binary processing function threshold() of opencv
牛客小白月赛7 谁是神箭手
Pythagorean number law (any three numbers can meet the conditions of Pythagorean theorem)
BCG 使用之CBCGPTabWnd控件(相当于MFC TabControl)
Niuke Xiaobai month race 7 who is the divine Archer
TCP waves twice, have you seen it? What about four handshakes?
随机推荐
需求开发思考
Specify the character set to output
上线首月,这家露营地游客好评率高达99.9%!他是怎么做到的?
有关架构设计的个人思考(本文后续不断修改更新)
1003 Emergency(25 分)(PAT甲级)
Mysql database basic operation -ddl | dark horse programmer
1011 World Cup betting (20 points) (pat a)
Cbcgpprogressdlgctrl progress bar used by BCG
node_ Exporter deployment
Pythagorean number law (any three numbers can meet the conditions of Pythagorean theorem)
HDU 1372 & POJ 2243 Knight moves (breadth first search)
做社交媒体营销应该注意些什么?Shopline卖家的成功秘笈在这里!
安徽 中安在线文旅频道推出“跟着小编游安徽”系列融媒体产品
Chrome development tool: what the hell is vmxxx file
c# . Net MVC uses Baidu ueditor rich text box to upload files (pictures, videos, etc.)
BCG 使用之CBCGPProgressDlgCtrl進度條使用
Matrix flip (array simulation)
1005 spell it right (20 points) (pat a)
数据集划分
ACM组合计数入门