Situation:
I have a login table with columns email and login_time. I would like to calculate the daily login time (DAU) and the rolling monthly login time (MAU). The DAU and MAU must count the distinct users. i.e. If someone logs in 20 times in the past 30 days (MAU) then he is only counted once. Same logic goes for DAU. MAU's range is 30 days.
DAU: is calculated by taking the distinct email login per day.
MAU: is calculated by taking the distinct email login for the rolling 30 days.
Desired Result: see fiddle below
Date MAU DAU
2019-04-01 4 2
2019-04-02 3 2
2019-04-03 4 2
Calculating the DAU is rather simple, however getting the rolling MAU, not sure how.
Fiddle:
create table #t1 (email varchar(max), login_time datetime)
insert into #t1 values
('[email protected]', '2019-03-15 00:00:00.000'),
('[email protected]', '2019-04-01 00:00:00.000'),
('[email protected]', '2019-04-02 00:00:00.000'),
('[email protected]', '2019-04-03 00:00:00.000'),
('[email protected]', '2019-03-19 00:00:00.000'),
('[email protected]', '2019-04-01 00:00:00.000'),
('[email protected]', '2019-04-02 00:00:00.000'),
('[email protected]', '2019-04-02 00:00:00.000'),
('[email protected]', '2019-03-02 00:00:00.000'),
('[email protected]', '2019-04-03 00:00:00.000'),
('[email protected]', '2019-03-06 00:00:00.000')