当前位置:网站首页>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
边栏推荐
- Matrix flip (array simulation)
- "Only one trip", active recommendation and exploration of community installation and maintenance tasks
- Stream stream
- 牛客小白月赛7 谁是神箭手
- 1008 elevator (20 points) (PAT class a)
- Thinking on demand development
- 【问题】druid报异常sql injection violation, part alway true condition not allow 解决方案
- Mysql database basic operation -ddl | dark horse programmer
- 数据集划分
- Niuke Xiaobai month race 7 who is the divine Archer
猜你喜欢
The explain statement in MySQL queries whether SQL is indexed, and several types in extra collate and summarize
实战模拟│JWT 登录认证
实战模拟│JWT 登录认证
[problem] Druid reports exception SQL injection violation, part always true condition not allow solution
c# . Net MVC uses Baidu ueditor rich text box to upload files (pictures, videos, etc.)
YOLOv5s-ShuffleNetV2
node_ Exporter deployment
Hough transform Hough transform principle
Oracle with as ora-00903: invalid table name multi report error
How to use async Awati asynchronous task processing instead of backgroundworker?
随机推荐
公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
Chrome development tool: what the hell is vmxxx file
1005 spell it right (20 points) (pat a)
Data set division
1008 elevator (20 points) (PAT class a)
Educational Codeforces Round 22 E. Army Creation
YOLOv5s-ShuffleNetV2
1009 Product of Polynomials(25 分)(PAT甲级)
Utilisation de la barre de progression cbcggprogressdlgctrl utilisée par BCG
What should we pay attention to when doing social media marketing? Here is the success secret of shopline sellers!
Online text line fixed length fill tool
In operation (i.e. included in) usage of SSRs filter
Jetpack Compose 教程
There are multiple divs in the large div, which are displayed on the same line. After overflow, scroll bars are generated without line breaks
【问题】druid报异常sql injection violation, part alway true condition not allow 解决方案
Double colon function operator and namespace explanation
有关架构设计的个人思考(本文后续不断修改更新)
求2的n次方
Cbcgptabwnd control used by BCG (equivalent to MFC TabControl)
Siemens HMI download prompts lack of panel image solution