我一直致力于客户流失分析。我根据以下代码成功估算了每月流失率。这是我用过的代码
with monthly_usage as (
select
who_identifier,
datediff(month, '1970-01-01', when_timestamp) as time_period
from events
where event = 'login' group by 1,2 order by 1,2),
lag_lead as (
select who_identifier, time_period,
lag(time_period,1) over (partition by who_identifier order by who_identifier, time_period),
lead(time_period,1) over (partition by who_identifier order by who_identifier, time_period)
from monthly_usage),
lag_lead_with_diffs as (
select who_identifier, time_period, lag, lead,
time_period-lag lag_size,
lead-time_period lead_size
from lag_lead),
calculated as (select time_period,
case when lag is null then 'NEW'
when lag_size = 1 then 'ACTIVE'
when lag_size > 1 then 'RETURN'
end as this_month_value,
case when (lead_size > 1 OR lead_size IS NULL) then 'CHURN'
else NULL
end as next_month_churn,
count(distinct who_identifier)
from lag_lead_with_diffs
group by 1,2,3)
select time_period, this_month_value, sum(count)
from calculated group by 1,2
union
select time_period+1, 'CHURN', count
from calculated where next_month_churn is not null
order by 1
但是,我有兴趣计算60天,90天和每日流失。有什么方法可以调整这段代码吗?我是SQL新手,目前正在研究MySQL。我共享的代码已经转换为MySQL,输出如下。