当前位置:网站首页>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
边栏推荐
- PointNeXt:通过改进的模型训练和缩放策略审视PointNet++
- mysql中explain语句查询sql是否走索引,extra中的几种类型整理汇总
- 项目中遇到的线上数据迁移方案1---总体思路整理和技术梳理
- HDU 1372 & POJ 2243 Knight Moves(广度优先搜索)
- 双冒号作用运算符以及命名空间详解
- Multi table operation inner join query
- Write it down once Net analysis of thread burst height of an industrial control data acquisition platform
- Comment utiliser async awati asynchrone Task Handling au lieu de backgroundworker?
- Euler function
- kotlin 循环控制
猜你喜欢
Dark horse programmer - software testing - stage 08 2-linux and database-23-30-process port related, modify file permissions, obtain port number information, program and process related operations, Li
92.(cesium篇)cesium楼栋分层
解密函数计算异步任务能力之「任务的状态及生命周期管理」
ACM组合计数入门
PointNeXt:通过改进的模型训练和缩放策略审视PointNet++
Upgrade the smart switch, how much is the difference between the "zero fire version" and "single fire" wiring methods?
Several methods of online database migration
CANN算子:利用迭代器高效实现Tensor数据切割分块处理
There are multiple divs in the large div, which are displayed on the same line. After overflow, scroll bars are generated without line breaks
C # use stopwatch to measure the running time of the program
随机推荐
Niuke Xiaobai month race 7 e applese's super ability
How to use async Awati asynchronous task processing instead of backgroundworker?
Dark horse programmer - software testing - 09 stage 2-linux and database -31-43 instructions issued by modifying the file permission letter, - find the link to modify the file, find the file command,
Jetpack compose tutorial
ACM组合计数入门
【毕业季】绿蚁新醅酒,红泥小火炉。晚来天欲雪,能饮一杯无?
Creation of JVM family objects
Crawler (6) - Web page data parsing (2) | the use of beautifulsoup4 in Crawlers
socket编程demo二
Stream stream
如何使用Async-Awati异步任務處理代替BackgroundWorker?
HDU 6440 2018 Chinese college student program design network competition
c# .net mvc 使用百度Ueditor富文本框上传文件(图片,视频等)
Double colon function operator and namespace explanation
kotlin 条件控制
HDU 1097 A hard puzzle
"Only one trip", active recommendation and exploration of community installation and maintenance tasks
1007 maximum subsequence sum (25 points) (PAT class a)
In operation (i.e. included in) usage of SSRs filter
Thinking on demand development