Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rolling Failure Rate With SQL

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

Your question is Rolling Failure Rate With SQL. 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

Business Context

Atlas Copco SMARTLINK provides operational data for monitoring industrial equipment. The service team wants to identify machines with elevated failure rates by tracking daily rates and their rolling 30-day averages.

Task

Write a PostgreSQL query that calculates the daily failure rate and rolling average failure rate for active SMARTLINK-connected machines between 2025-01-01 and 2025-02-28.

Requirements

  1. Join machine metadata with daily operational metrics and include only active machines.
  2. Calculate the daily failure rate as failures divided by inspections, expressed as a percentage. If inspections are zero or the failure count is unavailable, return NULL.
  3. Use a window function to calculate the average daily failure rate over the current date and the preceding 29 calendar days for each machine.
  4. Return results ordered by machine and metric date. A missing failure count must remain unavailable rather than being treated as zero.

Schema

smartlink_machines
ColumnTypeDescription
machine_idPKINTEGERUnique machine identifier
machine_nameVARCHAR(100)Machine name shown in SMARTLINK
is_activeBOOLEANWhether the machine is currently active
machine_daily_metrics
ColumnTypeDescription
metric_idPKINTEGERUnique daily metric record
machine_idINTEGERReferenced machine identifier
metric_dateDATEDate of the measurements
inspection_countINTEGERNumber of inspections
failure_countINTEGERNumber of failures, possibly unavailable
Tablessmartlink_machinesmachine_daily_metrics
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results