当前位置:网站首页>偏移量函数及开窗函数
偏移量函数及开窗函数
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、子查询用别名
边栏推荐
- [发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0
- 自由小兵儿
- Go microservice (II) - detailed introduction to protobuf
- 26. Delete the duplicate item C solution in the ordered array
- From automation to digital twins, what can Tupo do?
- Li Chi's work and life summary in June 2022
- Scala basic tutorial -- 18 -- set (2)
- 发送和接收IBM WebSphere MQ消息
- 反射(一)
- Go微服务(二)——Protobuf详细入门
猜你喜欢
与二值化阈值处理相关的OpenCV函数、方法汇总,便于对比和拿来使用
Scala basic tutorial -- 19 -- actor
2022CoCa: Contrastive Captioners are Image-Text Fountion Models
To sort out messy header files, I use include what you use
“只跑一趟”,小区装维任务主动推荐探索
关于判断点是否位于轮廓内的一点思考
[发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0
在线SQL转Excel(xls/xlsx)工具
Nebula Importer 数据导入实践
更安全、更智能、更精致,长安Lumin完虐宏光MINI EV?
随机推荐
完善的js事件委托
IBM WebSphere MQ检索邮件
每日一题(2022-07-02)——最低加油次数
IBM WebSphere MQ retrieving messages
Caché JSON 使用JSON适配器
1672. Total assets of the richest customers
请教一下 flinksql中 除了数据统计结果是状态被保存 数据本身也是状态吗
如何使用Async-Awati异步任务处理代替BackgroundWorker?
Summary and sorting of 8 pits of redis distributed lock
Pytest 可视化测试报告之 Allure
Using SSH
Is the securities account opened by qiniu safe?
利用策略模式优化if代码【策略模式】
2022CoCa: Contrastive Captioners are Image-Text Fountion Models
2021 合肥市信息学竞赛小学组
启牛开的证券账户安全吗?
Technologie de base de la programmation Shell IV
Download the first Tencent technology open day course essence!
2021 Hefei informatics competition primary school group
英特尔集成光电研究最新进展推动共封装光学和光互连技术进步