Premium problem90. Rolling 30-Day Unique Users

Medium Locked

events(user_id, event_at)

For every calendar date from 2026-09-01 to 2026-09-10, return the number of distinct users active on that date or any of the previous 29 calendar dates.

The windows reach back into August, so events from before September still count.

Return date and rolling_30d_users, ordered by date. Every one of the ten dates appears, with 0 where the window is empty.

Distinct counts cannot be rolled up from daily distinct counts, because the same user on two days is one user, so the window has to be counted from the raw events.

Tables

events

user_id  event_at
-------  -------------------
5        2026-08-02 10:00:00
1        2026-08-15 10:00:00
2        2026-08-20 10:00:00
1        2026-09-01 10:00:00
1        2026-09-02 10:00:00
3        2026-09-03 10:00:00
1        2026-09-05 10:00:00
4        2026-09-09 10:00:00

Expected result

date        rolling_30d_users
----------  -----------------
2026-09-01  2
2026-09-02  2
2026-09-03  3
2026-09-04  3
2026-09-05  3
2026-09-06  3
2026-09-07  3
2026-09-08  3
... 2 more row(s)

Premium problem

This one's part of Premium. Unlock the full MySQL track plus every other premium problem on the site.

Write one SELECT query