当前位置:网站首页>偏移量函数及开窗函数
偏移量函数及开窗函数
2022-07-04 17:53:00 【阳光下的青柠小镇】
案例
计算用户的平均次日留存率
建表语句
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');
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
注意的几个问题:
1、if函数
2、lead函数,lead(列或函数,往后多少行不填默认1,缺省值默认null)
3、distinct作用于多列,实际上根据多列去重的;
4、子查询用别名
边栏推荐
- Nebula Importer 数据导入实践
- repeat_P1002 [NOIP2002 普及组] 过河卒_dp
- 建立自己的网站(15)
- Use canal and rocketmq to listen to MySQL binlog logs
- Build your own website (15)
- 神经网络物联网应用技术学什么
- C#实现定义一套中间SQL可以跨库执行的SQL语句(案例详解)
- The latest progress of Intel Integrated Optoelectronics Research promotes the progress of CO packaging optics and optical interconnection technology
- 关于判断点是否位于轮廓内的一点思考
- 2022养生展,健康展,北京大健康展,健康产业展11月举办
猜你喜欢
随机推荐
反射(一)
使用SSH
To sort out messy header files, I use include what you use
Qt实现界面滑动切换效果
如何使用Async-Awati异步任务处理代替BackgroundWorker?
26. Delete the duplicate item C solution in the ordered array
整理混乱的头文件,我用include what you use
神经网络物联网是什么意思通俗的解释
2021 Hefei informatics competition primary school group
Summary and sorting of 8 pits of redis distributed lock
C#实现定义一套中间SQL可以跨库执行的SQL语句(案例详解)
OpenCV的二值化处理函数threshold()详解
Basic tutorial of scala -- 16 -- generics
Shell 編程核心技術《四》
测试工程师如何“攻城”(下)
自由小兵儿
Leetcode ransom letter C # answer
物联网应用技术的就业前景和现状
LeetCode 赎金信 C#解答
Using FTP