Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Running Totals and Moving Averages

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

Your question is Running Totals and Moving Averages. 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

CNA's claims analytics team needs monthly monitoring of incurred policy claims. Write a PostgreSQL query that calculates a cumulative claim total and a rolling twelve-month average by line of business.

Requirements

  1. Join claims to policies using policy_id.
  2. Aggregate claim amounts by line of business and calendar month from February 2024 through December 2024.
  3. Return monthly claims, a running total, and a moving average covering the current month and the prior eleven months.
  4. Treat NULL claim amounts as zero and order results chronologically.

Schema

policies
ColumnTypeDescription
policy_idPKINTEGERPrimary key for the policy
policy_numberVARCHAR(20)CNA policy identifier
line_of_businessVARCHAR(40)Policy business segment
effective_dateDATEPolicy effective date
claims
ColumnTypeDescription
claim_idPKINTEGERPrimary key for the claim
policy_idINTEGERReferenced policy
claim_dateDATEDate the claim was incurred
claim_amountNUMERIC(12,2)Incurred claim amount
Tablespoliciesclaims
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results