Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rolling Average Outage Duration

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

Your question is Rolling Average Outage Duration. Start with the requirements and the two 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

PG&E reliability analysts need to monitor how outage duration changes over time across service areas. Write a PostgreSQL query that calculates the rolling average duration of outages for each mapped PG&E service area.

Use a 30-day inclusive window, meaning each outage includes qualifying outages from the current timestamp and the previous 29 days. Exclude outage events that do not map to a known service area.

Requirements

  1. Join outage_events to service_areas using service_area_id.
  2. Calculate each outage duration in hours from started_at and ended_at; preserve NULL durations when an outage has not ended.
  3. Use a window function partitioned by service area and ordered by started_at to calculate the rolling average.
  4. Round individual durations and rolling averages to two decimal places, and order the output by service area and start time.

Schema

outage_events
ColumnTypeDescription
outage_idPKINTEGERUnique outage event identifier
service_area_idINTEGERReferences the PG&E service area
started_atTIMESTAMPOutage start timestamp
ended_atTIMESTAMPOutage end timestamp
causeVARCHAR(100)Reported outage cause
service_areas
ColumnTypeDescription
service_area_idPKINTEGERUnique service area identifier
service_areaVARCHAR(100)PG&E service area name
Tablesoutage_eventsservice_areas
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results