Skip to content

Commit e8ab377

Browse files
committed
find_churn_risk_customers.sql
1 parent 1f2279c commit e8ab377

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-- Write your PostgreSQL query statement below
2+
WITH cte AS (
3+
SELECT DISTINCT
4+
user_id,
5+
FIRST_VALUE(plan_name) OVER (PARTITION BY user_id ORDER BY event_date DESC) current_plan,
6+
FIRST_VALUE(event_type) OVER (PARTITION BY user_id ORDER BY event_date DESC) current_type,
7+
FIRST_VALUE(monthly_amount) OVER (PARTITION BY user_id ORDER BY event_date DESC) current_monthly_amount,
8+
MAX(monthly_amount) OVER (PARTITION BY user_id) max_historical_amount,
9+
FIRST_VALUE(event_date) OVER (PARTITION BY user_id ORDER BY event_date DESC) -
10+
FIRST_VALUE(event_date) OVER (PARTITION BY user_id ORDER BY event_date) days_as_subscriber
11+
FROM subscription_events)
12+
SELECT
13+
user_id,
14+
current_plan,
15+
current_monthly_amount,
16+
max_historical_amount,
17+
days_as_subscriber
18+
FROM cte
19+
WHERE current_type != 'cancel'
20+
AND current_monthly_amount < max_historical_amount / 2.0
21+
AND days_as_subscriber >= 60
22+
ORDER BY days_as_subscriber DESC, user_id

0 commit comments

Comments
 (0)