当前位置:网站首页>MySQL calculate n-day retention rate
MySQL calculate n-day retention rate
2022-07-02 15:26:00 【Caspian�】
One 、 Create a table containing the earliest login date of each user
select user_id,min(date) as first_day
from a2_userbehavior_csv
group by user_id
Two 、 Create a table containing all login dates of each user
In fact, it means de duplication of users and dates
select user_id,date
from a2_userbehavior_csv
group by user_id,date
3、 ... and 、 Set the two tables according to user_id Splicing , And calculate the date time difference
select t1.*,t2.date,datediff(t2.date,t1.first_day) as day_diff
from
(select user_id,min(date) as first_day
from a2_userbehavior_csv
group by user_id) as t1
left join
(select user_id,date
from a2_userbehavior_csv
group by user_id,date) as t2
on t1.user_id=t2.user_id
The results are as follows :
Get the number of days from each login date of each user to its earliest login date .
Four 、 Calculate various retention rates
Now the train of thought is clear .
Next day retention rate =(day_diff=1 The number of )/(day_diff=0 The number of )
Three day retention rate =(day_diff=3 The number of )/(day_diff=0 The number of )
...
select first_day as dt,
concat(round(100*count(case when day_diff=1 then user_id end)/count(case when day_diff=0 then user_id end),2),"%") as ' Next day retention rate ',
concat(round(100*count(case when day_diff=3 then user_id end)/count(case when day_diff=0 then user_id end),2),"%") as ' Three day retention rate ',
concat(round(100*count(case when day_diff=7 then user_id end)/count(case when day_diff=0 then user_id end),2),"%") as ' Seven day retention rate '
from
(select t1.*,t2.date,datediff(t2.date,t1.first_day) as day_diff
from
(select user_id,min(date) as first_day
from a2_userbehavior_csv
group by user_id) as t1
left join
(select user_id,date
from a2_userbehavior_csv
group by user_id,date) as t2
on t1.user_id=t2.user_id) as t3
group by first_day
order by first_day
边栏推荐
猜你喜欢
03.golang初步使用
How to find a sense of career direction
03_线性表_链表
做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
kibana 基础操作
List set & UML diagram
How to choose a third-party software testing organization for automated acceptance testing of mobile applications
List集合&UML图
Application and practice of Jenkins pipeline
Kibana basic operation
随机推荐
百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
03.golang初步使用
TiDB数据迁移场景综述
HUSTPC2022
LeetCode刷题——两整数之和#371#Medium
04_ 栈
如何用 Sysbench 测试 TiDB
FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
.NET Core 日志系统
Kibana basic operation
I made an istio workshop. This is the first introduction
There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant
18_ Redis_ Redis master-slave replication & cluster building
TiDB 软件和硬件环境建议配置
03_线性表_链表
Tidb cross data center deployment topology
18_Redis_Redis主从复制&&集群搭建
使用 TiUP 部署 TiDB 集群
Tidb environment and system configuration check
XML配置文件