Created
May 27, 2022 15:02
-
-
Save Intelrunner/659c99a5710598a137e72659465c0570 to your computer and use it in GitHub Desktop.
Calculate Churn Rate
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
WITH | |
[CHURN STAT] AS | |
(SELECT * | |
FROM [TABLE] | |
WHERE [STAT START] < '[DATE]' | |
AND ( | |
([STAT END] >= '2018-01-01') | |
OR ([STAT END]] IS NULL)) | |
), | |
status AS | |
(SELECT | |
CASE | |
WHEN ([STAT END] > [END DATE]) | |
OR ([STAT END] IS NULL) THEN 0 | |
ELSE 1 | |
END as is_canceled, | |
CASE | |
WHEN ([STAT START] < '2018-01-01') | |
AND ( | |
([STAT END] >= '2018-01-01') | |
OR ([STAT END] IS NULL) | |
) THEN 1 | |
ELSE 0 | |
END as is_active | |
FROM [OTHER TABLE] | |
) | |
SELECT 1.0 * SUM(is_canceled)/SUM(is_active) AS churn_rate | |
FROM status; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment