Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

7-Day Running Total SQL

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is 7-Day Running Total SQL. Start with the requirements and the one table on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Highmark Health analysts need a daily view of enrollment activity to monitor short-term changes across coverage operations. The enrollment source may contain missing dates, so the rolling calculation must use calendar days rather than simply counting the previous seven rows.

Write a PostgreSQL query that calculates the 7-day running enrollment total for January 1 through January 10, 2025.

Requirements

  1. Return enrollment_date, the daily enrollment count, and running_7_day_total.
  2. Include the current date and the six preceding calendar days in each total.
  3. Preserve dates with a NULL daily count, treating them as zero in the total.
  4. Return only the requested January reporting period, ordered chronologically. Enrollment records before January 1 may be used when they fall within a seven-day window.

Schema

daily_enrollments
ColumnTypeDescription
enrollment_datePKDATEDate of enrollment activity
enrollment_countINTEGERNumber of enrollments recorded on the date
Tablesdaily_enrollments
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results