Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Start free
SQL Running Totals and Averages
00:00
5 left

SQL Running Totals and Averages

HardSQL · PostgreSQL

Problem

Avathon's industrial monitoring system stores sensor readings for registered machines. Write a PostgreSQL query that summarizes readings for active machines during March 1 through March 10, 2025.

Requirements

  1. Join the machine registry to sensor readings and include only active machines in the requested date range.
  2. Aggregate multiple readings from the same machine and date into daily totals, counts, and averages.
  3. Calculate a chronological running total of readings for each machine.
  4. Calculate a trailing seven-calendar-day average, inclusive of the current date. Treat missing and NULL readings as unavailable rather than zero for the average.

Use RANGE BETWEEN INTERVAL '6 days' PRECEDING AND CURRENT ROW for the inclusive seven-day window. Preserve dates containing only NULL readings so the running total remains unchanged and the moving average excludes those NULL values.

Output details

  • Return columns in this order: machine_code, reading_date, daily_average, running_total, and trailing_7_day_avg.
  • Round daily_average, running_total, and trailing_7_day_avg to two decimal places.
  • Order rows by machine_code ascending, then reading_date ascending.
  • Treat zero reading counts as producing a NULL trailing_7_day_avg.

Schema

machines
ColumnTypeDescription
machine_idPKINTEGERUnique machine identifier
machine_codeVARCHAR(20)Avathon machine code
asset_nameVARCHAR(100)Human-readable asset name
statusVARCHAR(20)Current machine status
sensor_readings
ColumnTypeDescription
reading_idPKINTEGERUnique sensor reading identifier
machine_idINTEGERMachine that produced the reading
reading_dateDATECalendar date of the reading
reading_valueNUMERIC(10,2)Sensor measurement
Tablesmachinessensor_readings
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the two tables are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.