Restaurant Growth

4pts
Door Dash

Problem Statement

As a data analyst at DoorDash, you are tracking the growth of a local restaurant. You want to analyze the spending patterns using a 7-day moving window.

Write an SQL query to calculate the moving average of how much the customer paid in a seven-day window (current day + 6 days before).

Rules:

  • The window must include the current day and the previous 6 days.
  • The average_amount should be rounded to 2 decimal places.
  • The first 6 days of the restaurant's history should be excluded because they do not have a full 7-day window.
  • The result columns should be visited_on, amount (total for 7 days), and average_amount.
  • Results must be sorted by visited_on in ascending order.
Tests your understanding of
Basic SQL, Window Functions, Moving Average and Aggregation

Input Tables

customer
customer_id(INTEGER)name(VARCHAR)visited_on(DATE)amount(INTEGER)
1Jhon2019-01-01100
2Daniel2019-01-02110
3Jade2019-01-03120
4Khaled2019-01-04130
5Winston2019-01-05110
6Elvis2019-01-06140
7Anna2019-01-07150
8Maria2019-01-0880
9Jaze2019-01-09110
1Jhon2019-01-10130
3Jade2019-01-10150

Expected Output

visited_on(DATE)amount(INTEGER)average_amount(DECIMAL)
2019-01-07860122.86
2019-01-08840120
2019-01-09840120
2019-01-101000142.86

Tags

MediumBasic SQLWindow FunctionsMoving AverageAggregation
20-25 min
51%

Hints