用户留存率Cohort分析

SQL数据分析入门SQL

问题

按用户注册月份分组,计算各批次用户的次日、7日、30日留存率,生成留存矩阵。

答题要点

-- 用户注册月份Cohort
WITH user_cohorts AS (
    SELECT
        user_id,
        DATE_FORMAT(register_time, '%Y-%m') AS cohort_month,
        DATE(register_time) AS register_date
    FROM users
    WHERE register_time >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
),
-- 用户活跃记录
user_activity AS (
    SELECT DISTINCT
        user_id,
        DATE(login_time) AS active_date
    FROM user_login_log
    WHERE login_time >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
),
-- 计算留存
cohort_retention AS (
    SELECT
        uc.cohort_month,
        COUNT(DISTINCT uc.user_id) AS cohort_size,

        -- 次日留存
        COUNT(DISTINCT CASE
            WHEN DATEDIFF(ua.active_date, uc.register_date) = 1
            THEN uc.user_id
        END) AS day1_retained,

        -- 7日留存
        COUNT(DISTINCT CASE
            WHEN DATEDIFF(ua.active_date, uc.register_date) = 7
            THEN uc.user_id
        END) AS day7_retained,

        -- 30日留存
        COUNT(DISTINCT CASE
            WHEN DATEDIFF(ua.active_date, uc.register_date) = 30
            THEN uc.user_id
        END) AS day30_retained,

        -- 范围留存(7日内任意一天活跃)
        COUNT(DISTINCT CASE
            WHEN DATEDIFF(ua.active_date, uc.register_date) BETWEEN 1 AND 7
            THEN uc.user_id
        END) AS day1_7_retained,

        -- 范围留存(8-30日活跃)
        COUNT(DISTINCT CASE
            WHEN DATEDIFF(ua.active_date, uc.register_date) BETWEEN 8 AND 30
            THEN uc.user_id
        END) AS day8_30_retained

    FROM user_cohorts uc
    LEFT JOIN user_activity ua ON uc.user_id = ua.user_id
    GROUP BY uc.cohort_month
)
SELECT
    cohort_month,
    cohort_size,
    day1_retained,
    ROUND(100.0 * day1_retained / cohort_size, 2) AS day1_retention_rate,
    day7_retained,
    ROUND(100.0 * day7_retained / cohort_size, 2) AS day7_retention_rate,
    day30_retained,
    ROUND(100.0 * day30_retained / cohort_size, 2) AS day30_retention_rate,
    ROUND(100.0 * day1_7_retained / cohort_size, 2) AS day1_7_range_retention_rate,
    ROUND(100.0 * day8_30_retained / cohort_size, 2) AS day8_30_range_retention_rate
FROM cohort_retention
ORDER BY cohort_month DESC;

-- 留存曲线(更详细的每日留存)
WITH RECURSIVE days AS (
    SELECT 0 AS day_offset
    UNION ALL
    SELECT day_offset + 1 FROM days WHERE day_offset < 30
),
cohort_daily_retention AS (
    SELECT
        uc.cohort_month,
        d.day_offset,
        COUNT(DISTINCT uc.user_id) AS cohort_size,
        COUNT(DISTINCT CASE
            WHEN DATEDIFF(ua.active_date, uc.register_date) = d.day_offset
            THEN uc.user_id
        END) AS retained_users
    FROM user_cohorts uc
    CROSS JOIN days d
    LEFT JOIN user_activity ua ON uc.user_id = ua.user_id
    GROUP BY uc.cohort_month, d.day_offset
)
SELECT
    cohort_month,
    day_offset,
    cohort_size,
    retained_users,
    ROUND(100.0 * retained_users / cohort_size, 2) AS retention_rate
FROM cohort_daily_retention
WHERE cohort_month >= DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 3 MONTH), '%Y-%m')
ORDER BY cohort_month, day_offset;

完整解析在题库里看

核心知识点、易错点和面试官想听到什么;练过的题会进入记录,答错的进错题本。

去练这道题

回知识库看这一块

同一能力的其他题