Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rolling Average SQL for Test Results

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

Your question is Rolling Average SQL for Test Results. Start with the requirements and the three 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

TÜV SÜD quality teams monitor inspection measurements across testing facilities. Write a PostgreSQL query that calculates a rolling average of each test result over the current date and the previous 29 calendar days.

Requirements

  1. Join test results to the facility and test catalog tables.
  2. Partition the rolling average by facility and test type.
  3. Use a time-based window, not a row-count window, so gaps between test dates are handled correctly.
  4. Return the result value and rolling average for every matched test result, ordered by facility, test type, and test timestamp. Ignore NULL measurements in the average.

Schema

facilities
ColumnTypeDescription
facility_idPKINTEGERUnique testing facility identifier
facility_nameVARCHAR(100)TÜV SÜD testing facility name
test_catalog
ColumnTypeDescription
test_type_idPKINTEGERUnique test type identifier
test_type_nameVARCHAR(100)Inspection test name
test_results
ColumnTypeDescription
result_idPKINTEGERUnique test result identifier
facility_idINTEGERFacility reference
test_type_idINTEGERTest catalog reference
tested_atTIMESTAMPTest completion timestamp
result_valueNUMERIC(10,2)Measured test result
Tablesfacilitiestest_catalogtest_results
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results