不确定我是否理解正确:您想知道每个月有多少客户开始为第一个用户付费,有多少客户停止为最后一个用户付费?
解决方案看起来相当复杂,但也许并不那么容易。
with months as
(
select * from
generate_series('2014-06-01', now() at time zone 'utc', interval '1 month') as month
cross join paid_users
)
, sums as
(
select month, payor_id, joiners, leavers, sum(net) over (partition by payor_id order by month)
from
(
select month, payor_id, joiners, leavers, coalesce(joiners,0) - coalesce(leavers, 0) as net
from
(
select payor_id, month, count(*) as joiners
from months
where payment_start_date >= month
and payment_start_date < month + interval '1 month'
group by month, payor_id
) as t
full join
(
select payor_id, month, count(*) as leavers
from months
where payment_stop_date >= month
and payment_stop_date < month + interval '1 month'
group by month, payor_id
) as u
using (month, payor_id)
) as v
)
select * from sums
order by payor_id, sum
以上内容应为您提供每位客户的付费用户总数
month | payor_id | joiners | leavers | sum
---------------------+----------+---------+---------+-----
2014-06-01 00:00:00 | 1725 | 1 | | 1
2014-06-01 00:00:00 | 1929 | 1 | | 1
2015-10-01 00:00:00 | 1929 | | 1 | 0
2014-06-01 00:00:00 | 1986 | 1 | | 1
2014-11-01 00:00:00 | 3453 | 2 | | 2
2014-12-01 00:00:00 | 3453 | | 2 | 0
2015-01-01 00:00:00 | 3453 | 1 | | 1
2015-03-01 00:00:00 | 3453 | 1 | | 2
2015-04-01 00:00:00 | 3453 | 2 | 1 | 3
2015-05-01 00:00:00 | 3453 | | 1 | 2
2015-06-01 00:00:00 | 3453 | | 1 | 1
2015-10-01 00:00:00 | 3453 | 1 | | 2
2015-07-01 00:00:00 | 6499 | 1 | | 1
2015-08-01 00:00:00 | 6499 | 3 | | 4
2015-10-01 00:00:00 | 6499 | | 1 | 3
2015-11-01 00:00:00 | 6499 | | 1 | 2
所以新客户是总和为 0 到非零总和的客户,流失客户是总和为 0 的客户?
select month, new, churned from
(
(
select month, count(*) as churned
from sums
where sum = 0
group by month
) as l
full join
(
select month, count(*) as new
from (
select month, payor_id, sum, coalesce(lag(sum) over (partition by payor_id order by month), 0) as prev_sum
from sums
order by payor_id, month
) as t
where prev_sum = 0 and sum > 0
group by month
) as r
using (month)
)
order by month
输出
month | new | churned
---------------------+-----+---------
2014-06-01 00:00:00 | 3 |
2014-11-01 00:00:00 | 1 |
2014-12-01 00:00:00 | | 1
2015-01-01 00:00:00 | 1 |
2015-07-01 00:00:00 | 1 |
2015-10-01 00:00:00 | | 1
希望这会有所帮助。如果有人知道更简单的方法,我会很高兴听到它。