File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments