Skip to content

Instantly share code, notes, and snippets.

@Birappa87
Created August 13, 2022 11:40
Show Gist options
  • Save Birappa87/099310868c3a80b6b120516761540d5a to your computer and use it in GitHub Desktop.
Save Birappa87/099310868c3a80b6b120516761540d5a to your computer and use it in GitHub Desktop.
-- 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