Skip to content
Onsite

Rolling 7-day distinct active users

HardP3Coding

Table activity:

ColumnType
user_idint
activity_datedate

A user can have several rows on the same day.

For every calendar day from the first to the last day in the table, return the number of distinct users who were active on that day or any of the 6 days before it (a 7-day window ending on that day). Days with nobody active in the window have count 0.

Output columns: day, active_users. Order by day.

Schema

CREATE TABLE activity (
  user_id int NOT NULL,
  activity_date date NOT NULL
);

Example 1

Input
user A active Jan 1; user B active Jan 9
Output
Jan 1 to Jan 7: 1, Jan 8: 0, Jan 9: 1
Why
On Jan 8 the window is Jan 2 to Jan 8, which has no activity.

Constraints

  • PostgreSQL dialect.

Hints

PostgreSQL
Run checks the 3 visible tests, Submit all 6.