I have a table of policies with effective & expiration dates. I want to know how many policies were in effect during each month.
Raw Table
id, effective_date, expiration_date
1, 2020-01-01, 2020-06-01
2, 2020-02-01, 2020-07-01
3, 2020-03-01, 2020-08-01
4, 2020-04-01, 2020-09-01
5, 2020-04-01, 2020-09-01
Ideal Output
Month, Count
Jan, 1
Feb, 2
March, 3
April, 5
May, 5
June, 5
July, 4
August, 3
Sept, 2
I was thinking I could do something like the following
Effective_Count
RUNNING_COUNT(COUNT([policies]))
Expired_Count
RUNNING_COUNT(COUNT(
IF(policy.expiration_date < RowMonth)
THEN 1
ELSE 0
END
))
And then the policies_in_effect would just be
Effective_Count - Expired_Count
I'm not able to get the RowMonth value though.