SQL面试题-留存率计算

表定义:

sql 复制代码
create table if not exists liuliang_detail
(
    user_id string comment ''
    ,record_time string comment 'yyyymmdd hh:mi:ss'
)
comment '流量明细表'
;

方法一:

计算的是整段时间范围内,每一天为基准的所有的留存1、2、7天的用户数。

方法一的优势是可以一次性计算出,每天的不同时间范围的留存率。

但是不是很直观,并且计算量比较大。

sql 复制代码
# 按照用户的访问时间进行排序
create table if not exists liuliang_partition as
select a.user_id
       ,a.record_time
       ,row_number() over(partition by user_id order by record_time) rn_asc
       --,row_number() over(partition by user_id order by recordtime desc) rn_des
from liuliang_detail a
where date(record_time) >= '2021-01-01' -- 最好根据产品上线时间确定,要不然流量表太大,影响运行效率
;

# 计算整段时间范围内,以每天为基准的的留存率

select recorddate

       ,count(distinct user_id) total_uv

       ,count(distinct case when rn_asc = 1 then user_id else null end) new_uv -- 首次访问uv

       ,round(100*count(distinct case when rn_asc = 1 then user_id else null end)/count(distinct user_id), 1) new_uv_ratio -- 首次访问uv占比

       ,count(distinct case when rn_asc <> 1 and diff_days = 1 then user_id else null end) lastday_uv -- 次日留存

       ,count(distinct case when rn_asc <> 1 and diff_days = 2 then user_id else null end) last2day_uv -- 2日留存

       ,count(distinct case when rn_asc <> 1 and diff_days = 3 then user_id else null end) last3day_uv -- 3日留存

       ,count(distinct case when rn_asc <> 1 and diff_days = 4 then user_id else null end) last4day_uv -- 4日留存

       ,count(distinct case when rn_asc <> 1 and diff_days = 5 then user_id else null end) last5day_uv -- 5日留存

       ,count(distinct case when rn_asc <> 1 and diff_days = 6 then user_id else null end) last6day_uv -- 6日留存

       ,count(distinct case when rn_asc <> 1 and diff_days = 7 then user_id else null end) last7day_uv -- 7日留存

       ,count(distinct case when rn_asc <> 1 and diff_days = 14 then user_id else null end) last14day_uv -- 14日留存

       ,count(distinct case when rn_asc <> 1 and diff_days = 30 then user_id else null end) last30day_uv -- 30日留存

from

(

  select a.*

         ,date(record_time) recorddate

         ,datediff(cast(a.record_time as date), cast(b.record_time as date)) diff_days -- 留存天数

  from liuliang_partition a

  left join liuliang_partition b on a.user_id = b.user_id and a.rn_asc = b.rn_asc+1

) x

group by recorddate;

方法二:

计算的是用户首次登陆时间为基准时间,计算该基准时间之后的n日留存率。

优点:代码直观好理解

缺点:如果要计算n天留存需要增加代码量

sql 复制代码
-- 计算次日留存率
WITH FirstLogin AS (
    -- 找出每个用户的首次登录时间
    SELECT
        user_id,
        MIN(record_time) AS first_record_time
    FROM
        user_log
    GROUP BY
        user_id
),
RetentionUsers AS (
    -- 找出次日登录的用户
    SELECT
        a.user_id,
        a.record_time,
        DATE_ADD(b.first_record_time, INTERVAL 1 DAY) AS expected_next_day
    FROM
        user_log a
    JOIN
        FirstLogin b ON a.user_id = b.user_id
    WHERE
        DATE(a.record_time) = DATE(expected_next_day)
)
-- 计算留存率
SELECT
    COUNT(DISTINCT RetentionUsers.user_id) AS next_day_retention_users,
    COUNT(DISTINCT FirstLogin.user_id) AS initial_users,
    ROUND(COUNT(DISTINCT RetentionUsers.user_id) / COUNT(DISTINCT FirstLogin.user_id) * 100, 2) AS next_day_retention_rate
FROM
    FirstLogin
LEFT JOIN
    RetentionUsers ON FirstLogin.user_id = RetentionUsers.user_id;
相关推荐
JSON_L30 分钟前
MySQL 事务处理
数据库·mysql
爱打lan球的程序员2 小时前
redis分布式锁和lua脚本
数据库·redis·分布式
说书客啊2 小时前
计算机毕业设计 | springboot旅行旅游网站管理系统(附源码)
java·数据库·spring boot·后端·毕业设计·课程设计·旅游
hummhumm2 小时前
数据库系统 第46节 数据库版本控制
java·javascript·数据库·python·sql·json·database
ac-er88882 小时前
Flask如何创建并运行数据库迁移
数据库·python·flask
传而习乎3 小时前
【Postgresql】地理空间数据的存储与查询,查询效率优化策略,数据类型与查询速度的影响
数据库·postgresql
King.6243 小时前
SQLynx如何提高企业数据库安全?
数据库·sql·mysql·postgresql·oracle
夜夜亮晶晶5 小时前
MySQL——表操作
数据库·mysql
GGBondlctrl5 小时前
【MySQL】初识MySQL—MySQL是啥,以及如何简单操作???
数据库·mysql
追风20196 小时前
认识向量数据
大数据·数据库·人工智能