Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

7-Day Rolling Average with Window Functions

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

Your question is 7-Day Rolling Average with Window Functions. Start with the requirements and the four tables 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

Siemens Healthineers operations analysts need to monitor downtime for the SOMATOM CT product line. Write a PostgreSQL query that calculates daily unplanned operational downtime and its seven-day rolling average.

Requirements

  1. Restrict results to active equipment assigned to the SOMATOM CT product line.
  2. Include every date from 2025-01-01 through 2025-01-10, including dates with no downtime.
  3. Exclude planned downtime and ignore events from inactive equipment.
  4. Aggregate downtime across equipment for each date.
  5. Use a window function to calculate the average of the current day and six preceding calendar rows.
  6. Return results in ascending date order, with downtime values rounded to two decimal places.

Schema

product_lines
ColumnTypeDescription
product_line_idPKINTProduct line identifier
product_line_nameVARCHAR(100)Product line name
equipment
ColumnTypeDescription
equipment_idPKINTEquipment identifier
product_line_idINTAssigned product line
equipment_codeVARCHAR(30)Equipment code
statusVARCHAR(20)Equipment lifecycle status
downtime_events
ColumnTypeDescription
event_idPKINTDowntime event identifier
equipment_idINTEquipment affected by the event
downtime_dateDATEDate of downtime
downtime_minutesNUMERIC(10,2)Duration of downtime in minutes
downtime_typeVARCHAR(20)Planned or unplanned downtime
calendar_dates
ColumnTypeDescription
calendar_datePKDATEDate in the reporting calendar
Tablesproduct_linesequipmentdowntime_eventscalendar_dates
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results