-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathfind-churn-risk-customers.sql
More file actions
31 lines (29 loc) · 996 Bytes
/
find-churn-risk-customers.sql
File metadata and controls
31 lines (29 loc) · 996 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Time: O(nlogn)
# Space: O(n)
# window function
WITH events_cte AS (
SELECT user_id,
event_date,
MAX(event_date) OVER (PARTITION BY user_id) AS max_event_date,
event_type,
plan_name,
monthly_amount,
MAX(monthly_amount) OVER (PARTITION BY user_id) AS max_historical_amount,
DATEDIFF(MAX(event_date) OVER (PARTITION BY user_id),
MIN(event_date) OVER (PARTITION BY user_id)
) AS days_as_subscriber
FROM subscription_events
)
SELECT user_id,
plan_name AS current_plan,
monthly_amount AS current_monthly_amount,
max_historical_amount,
days_as_subscriber
FROM events_cte AS e
WHERE event_date = max_event_date AND event_type != 'cancel'
AND EXISTS (
SELECT 1 FROM events_cte AS e2 WHERE e.user_id = e2.user_id AND e2.event_type = 'downgrade'
)
AND 2 * monthly_amount < max_historical_amount
AND days_as_subscriber >= 60
ORDER BY 5 DESC, 1;