Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rolling Three-Month Average Query

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

Your question is Rolling Three-Month Average Query. 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

Teradata Vantage reporting needs a monthly sales trend for active sales regions. Write a PostgreSQL-compatible SQL query that calculates monthly sales and a rolling three-month average for each active region from January through April 2025.

Requirements

  1. Aggregate transaction amounts by region and calendar month.
  2. Include months with no transactions as zero sales.
  3. Calculate the average for the current month and up to the two preceding calendar months within each region.
  4. Return results ordered by region name and month.

Schema

regions
ColumnTypeDescription
region_idPKINTUnique identifier for a sales region
region_nameVARCHAR(100)Display name of the sales region
is_activeBOOLEANWhether the region is included in reporting
sales_transactions
ColumnTypeDescription
transaction_idPKINTUnique transaction identifier
region_idINTRegion associated with the transaction
sale_dateDATEDate on which the sale occurred
amountDECIMAL(12,2)Sales amount, which may be NULL
Tablesregionssales_transactions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results