我已经创建了一个派生表,它应该非常接近于计算流失,但似乎无法获得最终答案
account_id current_month_paid last_month_paid next_month_paid
633 2018-10 NULL NULL
862 2018-10 NULL 2018-11
862 2018-11 2018-10 2018-12
862 2018-12 2018-11 2019-01
1448 2018-10 NULL 2018-11
如何对这些列进行分组,以便每月获得保留率,如下所示:
2018-10 2018-11 2018-12
92% 94% 93%
任何帮助都将不胜感激。我在使用AWS雅典娜,以防万一
编辑:这些是样本百分比,它们不是实际计算。我希望按月分组,并显示前一个月支付的百分比(每个帐户ID)
发布于 2020-07-05 03:15:16
派生表的语义对我来说不是很清楚,但假设
WITH paying_user_stats AS (
SELECT
account_id,
current_month_paid,
1 as paying_users,
case when last_month_paid is null then 1 else 0 end as paying_users_new,
case when next_month_paid is null then 1 else 0 end as paying_users_churn
FROM accounts
)
SELECT
current_month_paid,
sum(paying_users) as paying_users,
sum(paying_users-paying_users_new) as paying_users_month_begin,
sum(paying_users-paying_users_churn) as paying_users_month_end,
sum(paying_users_new) as paying_users_new,
sum(paying_users_churn) as paying_users_churn
FROM paying_user_stats
GROUP BY current_month_paid
现在,如果我没有理解错您对保留率的定义,您正在寻找的是sum(paying_users)/sum(paying_users_month_begin)
。您可能希望在BI工具、电子表格等查询之外执行该划分,以避免意外地将其聚合为未加权平均值等问题。
https://stackoverflow.com/questions/62706059
复制相似问题