当前位置:网站首页>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_idTwo 、 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,date3、 ... 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_idThe 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
边栏推荐
- XML Configuration File
- Tidb hybrid deployment topology
- 20_ Redis_ Sentinel mode
- . Net core logging system
- 20_Redis_哨兵模式
- 【网络安全】网络资产收集
- PHP method to get the index value of the array item with the largest key value in the array
- Jenkins Pipeline 应用与实践
- Case introduction and problem analysis of microservice
- The past and present lives of visual page building tools
猜你喜欢

03_ Linear table_ Linked list

Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?

飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
![[c voice] explain the advanced pointer and points for attention (2)](/img/fb/515e25899bd9a2905ee63cb041934a.png)
[c voice] explain the advanced pointer and points for attention (2)

Internet Explorer officially retired

基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测

6.12 企业内部upp平台(Unified Process Platform)的关键一刻

LeetCode刷题——两整数之和#371#Medium

Learn the method code example of converting timestamp to uppercase date using PHP

07_ Hash
随机推荐
LeetCode刷题——统计各位数字都不同的数字个数#357#Medium
TiDB 软件和硬件环境建议配置
Principles, language, compilation, interpretation
哈夫曼树:(1)输入各字符及其权值(2)构造哈夫曼树(3)进行哈夫曼编码(4)查找HC[i],得到各字符的哈夫曼编码
TiDB 集群最小部署的拓扑架构
LeetCode_ String_ Simple_ 412.Fizz Buzz
Deploy tidb cluster with tiup
搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
AtCoder Beginner Contest 254
基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
工程师评测 | RK3568开发板上手测试
Why can't programmers who can only program become excellent developers?
How to conduct TPC-C test on tidb
06_ Stack and queue conversion
How to solve the problem of database content output
. Net again! Happy 20th birthday
Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language
08_ strand
03_线性表_链表
I made an istio workshop. This is the first introduction