Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

7-Day Moving Average of Daily GMV

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

Your question is 7-Day Moving Average of Daily GMV. Start with the requirements and the one table 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

You’re a data engineer supporting the analytics team at a large e-commerce marketplace (~5M daily active users, ~10M order items/day). Finance uses daily GMV (gross merchandise value) to forecast revenue and detect demand shocks. However, daily GMV is noisy due to promotions and weekend effects, so stakeholders want a 7-day moving average to smooth the trend.

The source of truth is an orders table where each row is an order (not an item). Orders can be paid, refunded, or canceled. Only paid orders should contribute to GMV.

Task

Write a SQL query that returns daily paid GMV and its 7-day trailing moving average (including the current day) for a given date range.

Requirements

  1. Aggregate daily GMV as the sum of order_total_usd for orders where status = 'paid'.
  2. Return one row per order_date (UTC date) within 2024-01-01 to 2024-01-10 (inclusive).
  3. Compute gmv_7d_moving_avg as the average of daily_gmv_usd over the window current day and previous 6 days.
  4. If fewer than 7 prior days exist (early in the range), compute the average over the available days.
  5. Order results by order_date ascending.

Schema

orders
ColumnTypeDescription
order_idPKBIGINTPrimary key
user_idBIGINTBuyer user id
order_tsTIMESTAMPOrder creation timestamp in UTC
statusVARCHAR(20)Order status: paid, refunded, canceled
order_total_usdDECIMAL(10,2)Total order amount in USD
Tablesorders
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results