用户增长分析
SQL查询入门SQL用户增长分析
问题
分析每月新增用户数、累计用户数和月环比增长率。
答题要点
WITH monthly_new AS (
SELECT
DATE_FORMAT(MIN(register_date), '%Y-%m') AS cohort_month,
COUNT(*) AS new_users
FROM users
GROUP BY user_id
),
aggregated AS (
SELECT
cohort_month,
SUM(new_users) AS monthly_new_users,
SUM(SUM(new_users)) OVER (ORDER BY cohort_month) AS cumulative_users,
LAG(SUM(new_users)) OVER (ORDER BY cohort_month) AS prev_month_new
FROM monthly_new
GROUP BY cohort_month
)
SELECT
cohort_month,
monthly_new_users,
cumulative_users,
ROUND((monthly_new_users - prev_month_new) * 100.0 / NULLIF(prev_month_new, 0), 2) AS growth_rate
FROM aggregated
ORDER BY cohort_month;
完整解析在题库里看
核心知识点、易错点和面试官想听到什么;练过的题会进入记录,答错的进错题本。
去练这道题