📜  如何计算 SQL 中的保留率?

📅  最后修改于: 2022-05-13 01:54:30.619000             🧑  作者: Mango

如何计算 SQL 中的保留率?

留存率的计算方法是计算定期(例如每周或每月)返回的用户数量,并按注册的周数对其进行分组。

假设用户是 X School 的学生,每天都需要登录门户网站才能访问教师发布的学习资料,并且每个用户的详细信息(例如登录时间、用户 ID 和日期)都会在门户网站的服务器上更新。他们登录的时间。

现在我们有一个存储 user_id、login_time 和每个用户访问日期的表。

第 1 步:创建一个具有 3 列 'login_date、user_id、id' 的表登录,并使用 IDENTITY 关键字对设置为主键的 'id' 执行 auto_increment 它定义每次新记录时该列将自动递增 1被插入。

询问:

CREATE TABLE login(login_date DATE, user_id INT,
id INT not null IDENTITY, PRIMARY KEY(id));

第 2 步:使用 SQL 查询为登录表中的每一行插入值,

询问:

INSERT INTO login(login_date,user_id)
VALUES('2022-01-01',10),('2022-01-02',12),('2022-01-03',15),
('2022-01-04',11),('2022-01-05',13),('2022-01-06',9),
('2022-01-07',21),('2022-01-08',10),('2022-01-09',10),
('2022-01-10',2),('2022-01-11',16),('2022-01-12',12),
('2022-01-13',10),('2022-01-14',18),('2022-01-15',15),
('2022-01-16',12),('2022-01-17',10),('2022-01-18',18),
('2022-01-19',14),('2022-01-20',16),('2022-01-21',12),
('2022-01-22',21),('2022-01-23',13),('2022-01-24',15),
('2022-01-25',20),('2022-01-26',14),('2022-01-27',16),
('2022-01-28',15),('2022-01-29',10),('2022-01-30',18);

第 3 步:在每列的行中插入值后查看登录表。

询问:

SELECT * FROM login;

输出:

为了计算留存率,我们将按登录一周对每次访问进行分组。 Microsoft SQL Server 有一个函数DATEPART(),它返回指定日期的指定部分(年、季度、月、周、小时、分钟等)。

DATEPART()有两个返回周数据的参数,

  • 星期
  • 登录日期

询问:

SELECT user_id,DATEPART(week, login_date) 
AS login_week FROM login GROUP BY user_id, 
DATEPART(week, login_date);

输出:

现在,我们将使用 MIN函数和 GROUP BY 计算每个用户登录的第一周,以返回每个用户的第一个登录周。

询问:

SELECT user_id, min(DATEPART
(week, login_date)) AS first FROM 
login GROUP BY user_id;

输出:

使用 INNER JOIN 组的结果“login_week 和 first”来计算保留率。

询问:

Select m.user_id,m.login_week,n.first as first  from    
(SELECT user_id,DATEPART(week,login_date) AS login_week
FROM login GROUP BY user_id,DATEPART(week,login_date)) m,
(SELECT user_id,min(DATEPART(week,login_date)) AS first
FROM login GROUP BY user_id) n where m.user_id = n.user_id;

输出:

这里我们将得到login_week和first的差值来计算周数(week number)。

询问:

Select m.user_id,m.login_week,n.first as first,
m.login_week-first as week_number from    
(SELECT user_id, DATEPART(week,login_date) 
AS login_week FROM login GROUP BY user_id,
DATEPART(week,login_date)) m, (SELECT user_id,
min (DATEPART(week,login_date)) AS first FROM login
GROUP BY user_id) n where m.user_id=n.user_id;

输出:

在此之后,将结果组合到一个群组表中,该表的第一周有一行,每周数有一个列,其中有许多用户在“n”周后保留以登录门户。使用以下查询计算 SQL 中的保留率。对于群组表分析,请使用数据可视化工具。

询问:

Select first,
SUM(CASE WHEN week_number = 0 THEN 1 ELSE 0 END) AS week_0,
SUM(CASE WHEN week_number = 1 THEN 1 ELSE 0 END) AS week_1,
SUM(CASE WHEN week_number = 2 THEN 1 ELSE 0 END) AS week_2,
SUM(CASE WHEN week_number = 3 THEN 1 ELSE 0 END) AS week_3,
SUM(CASE WHEN week_number = 4 THEN 1 ELSE 0 END) AS week_4,
SUM(CASE WHEN week_number = 5 THEN 1 ELSE 0 END) AS week_5,
SUM(CASE WHEN week_number = 6 THEN 1 ELSE 0 END) AS week_6,
SUM(CASE WHEN week_number = 7 THEN 1 ELSE 0 END) AS week_7,
SUM(CASE WHEN week_number = 8 THEN 1 ELSE 0 END) AS week_8,
SUM(CASE WHEN week_number = 9 THEN 1 ELSE 0 END) AS week_9 
from (select m.user_id,m.login_week,n.first as first,
m.login_week-first as week_number  from   (SELECT
user_id, DATEPART(week,login_date) AS login_week FROM login 
GROUP BY user_id,DATEPART(week,login_date)) m,(SELECT user_id, 
min(DATEPART(week,login_date)) AS first FROM login GROUP BY user_id) 
n where m.user_id = n.user_id) as with_week_number
group by first order by first;

输出: