当前位置:网站首页>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
边栏推荐
- Abc229 summary (connected component count of the longest continuous character graph in the interval)
- Find the nth power of 2
- CANN算子:利用迭代器高效实现Tensor数据切割分块处理
- Cbcgpprogressdlg progress bar used by BCG
- 【毕业季】绿蚁新醅酒,红泥小火炉。晚来天欲雪,能饮一杯无?
- 1006 sign in and sign out (25 points) (PAT class a)
- English语法_名词 - 使用
- Kotlin inheritance
- HDU 6440 2018 Chinese college student program design network competition
- 记一次 .NET 某工控数据采集平台 线程数 爆高分析
猜你喜欢
Opencv functions and methods related to binary threshold processing are summarized for comparison and use
TCP waves twice, have you seen it? What about four handshakes?
Explore the contour drawing function drawcontours() of OpenCV in detail with practical examples
How to use async Awati asynchronous task processing instead of backgroundworker?
Chrome development tool: what the hell is vmxxx file
English grammar_ Noun - use
线上数据库迁移的几种方法
New wizard effect used by BCG
上线首月,这家露营地游客好评率高达99.9%!他是怎么做到的?
Swagger suddenly went crazy
随机推荐
HDU 6440 2018 Chinese college student program design network competition
Actual combat simulation │ JWT login authentication
Multi table operation inner join query
Kotlin inheritance
Utilisation de la barre de progression cbcggprogressdlgctrl utilisée par BCG
"Only one trip", active recommendation and exploration of community installation and maintenance tasks
实战模拟│JWT 登录认证
HMM隐马尔可夫模型最详细讲解与代码实现
HDU 1372 & POJ 2243 Knight Moves(广度优先搜索)
Data set division
2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)
HMM hidden Markov model and code implementation
应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设
如何使用Async-Awati异步任務處理代替BackgroundWorker?
公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
node_ Exporter deployment
Pointnet/Pointnet++点云数据集处理并训练
欧拉函数
偏移量函数及开窗函数
上线首月,这家露营地游客好评率高达99.9%!他是怎么做到的?