I have a table with following columns:
**name** | **start_date** | **termination_date** |
X1 | 2019-01-01 | NULL
X2 | 2018-05-01 | 2019-03-01
I want to figure out how many people have been subscribed per month. Meaning that the start_date was before month X and the termination_date was either afterwards or is null.
E.g. For '2019-01-01' I want every row where the start_date was before or at '2019-01-01' and the termination_date is either after '2019-01-01' or Null
I tried it with this query:
SELECT start_date,
sum(count(distinct name)) OVER (order by start_date)
from Table
where
is_active = TRUE
and (termination_date >= start_date or termination_date is null)
Group by start_date
Order by start_date;
However, when I checked some of the results by hand I saw that it was not returning the correct values but the sum() is to high.
I have no idea why this happens.
Can anyone point me in the rigth direction?
start_date?datedata-type If I want to know how many active users I had in 2019-02 I want both rows to be counted but if I ask for actives for 2019-04 I only want 1