当前位置:网站首页>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
边栏推荐
猜你喜欢

19_ Redis_ Manually configure the host after downtime

Solve the problem that El radio group cannot be edited after echo

05_ queue

Markdown tutorial

Oracle primary key auto increment

07_ Hash

17_ Redis_ Redis publish subscription

Leetcode - Search 2D matrix

vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)

yolo格式数据集处理(xml转txt)
随机推荐
XML配置文件
二叉树的遍历方式主要有:先序遍历、中序遍历、后序遍历、层次遍历。先序、中序、后序其实指的是父节点被访问的次序。若在遍历过程中,父节点先于它的子节点被访问,就是先序遍历;
Download blender on Alibaba cloud image station
Infra11199 database system
16_ Redis_ Redis persistence
百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
Oracle primary key auto increment
03_ Linear table_ Linked list
Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
How to find a sense of career direction
Real estate market trend outlook in 2022
03.golang初步使用
08_ strand
Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
16_Redis_Redis持久化
Sharp tool SPL for post SQL calculation
Yolov5 code reproduction and server operation
. Solution to the problem of Chinese garbled code when net core reads files
There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant
19_ Redis_ Manually configure the host after downtime