大家好,我是考100分的小小码 ,祝大家学习进步,加薪顺利呀。今天说一说原创声明:求连续90日每日的用户留存率 -「建议收藏」,希望您对编程的造诣更进一步.
业务端需要求连续90日每日的用户留存率改怎么计算呢???
一: 本文采用MySQL8.0环境
1: 建表数据 CREATE TABLE `user_login` ( `user_id` int NOT NULL, `login_date` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; MySQL8.0 或者 hive 执行
表里的数据如下:
user_id login_date
“1001” “2021-12-12”
“1002” “2021-12-12”
“1001” “2021-12-13”
“1001” “2021-12-14”
“1001” “2021-12-16”
“1002” “2021-12-16”
“1001” “2021-12-19”
“1002” “2021-12-17”
“1001” “2021-12-20”
“1002” “2021-12-14”
“1002” “2021-12-20”
“1003” “2021-12-12”
“1004” “2021-12-18”
“1005” “2021-12-20”
“1006” “2021-12-14”
“1007” “2021-12-14”
“1007” “2021-12-19”
二:执行SQL
select a.login_date 日期, datediff(b.login_date , a.login_date) as days, count(distinct if(datediff(a.login_date , a.login_date_min) = 0 ,a.user_id,null)) as 注册用户数, count(distinct if(datediff(b.login_date , a.login_date) != 0 ,if(datediff(b.login_date , a.login_date) != 0 and a.user_id = b.user_id, b.user_id,null), if(datediff(a.login_date , a.login_date_min) = 0 ,a.user_id,null))) as days日留存用户数 from ( select user_id,login_date,case when rn =1 then login_date else null end login_date_min from (select user_id,login_date, row_number() over(partition by user_id order by login_date) rn from user_login ) tmp ) a left join user_login b on b.login_date >=a.login_date and datediff(b.login_date ,a.login_date) <=90 group by a.login_date,days having 注册用户数 != 0 order by 日期,days asc
三:执行结果
日期 days 注册用户数 days日留存用户数 2021-12-12 0 3 3 2021-12-12 1 3 1 2021-12-12 2 3 2 2021-12-12 4 3 2 2021-12-12 5 3 1 2021-12-12 6 3 0 2021-12-12 7 3 1 2021-12-12 8 3 2 2021-12-14 0 2 2 2021-12-14 2 2 2 2021-12-14 3 2 1 2021-12-14 4 2 0 2021-12-14 5 2 2 2021-12-14 6 2 2 2021-12-18 0 1 1
2021-12-18 1 1 0
2021-12-18 2 1 0
2021-12-20 0 1 1
参考博客:感谢大佬分享
https://blog.csdn.net/qq_25002995/article/details/104928397?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~default-1-104928397-blog-120935637.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~default-1-104928397-blog-120935637.pc_relevant_default&utm_relevant_index=1
原文地址:https://www.cnblogs.com/guodong1789/archive/2022/07/29/16532069.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/4954.html