不知道我理解這個正確的:每個月你想知道有多少客戶開始爲他們的第一個用戶付費,有多少客戶停止爲他們的最後一個用戶付款?
解決方案似乎相當複雜,但也許並不是那麼容易。
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
希望這有助於。如果有人知道更簡單的方法,我很樂意聽到它。
這似乎很合理,你絕對解釋了這個問題的想法比我更好 - 非常感謝你的幫助!數字看起來是正確的,邏輯很有意義:) –