Created
August 13, 2022 11:40
-
-
Save Birappa87/099310868c3a80b6b120516761540d5a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Retrieve next plan's start date located in the next row based on current row | |
WITH next_plan_cte AS ( | |
SELECT | |
customer_id, | |
plan_id, | |
start_date, | |
LEAD(plan_id, 1) OVER( | |
PARTITION BY customer_id | |
ORDER BY plan_id) as next_plan | |
FROM subscriptions) | |
SELECT | |
COUNT(*) AS downgraded | |
FROM next_plan_cte | |
WHERE start_date <= '2020-12-31' | |
AND plan_id = 2 | |
AND next_plan = 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment