Restaurant Growth Problem
Description
LeetCode Problem 1321.
Table: Customer
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| customer_id | int |
| name | varchar |
| visited_on | date |
| amount | int |
+---------------+---------+
(customer_id, visited_on) is the primary key for this table.
This table contains data about customer transactions in a restaurant.
visited_on is the date on which the customer with ID (customer_id) have visited the restaurant.
amount is the total paid by a customer.
You are the restaurant owner and you want to analyze a possible expansion (there will be at least one customer every day).
Write an SQL query to compute moving average of how much customer paid in a 7 days window (current day + 6 days before) .
The query result format is in the following example:
Return result table ordered by visited_on.
The average_amount should be rounded to 2 decimal places, all dates are in the format ('YYYY-MM-DD').
Customer table:
+-------------+--------------+--------------+-------------+
| customer_id | name | visited_on | amount |
+-------------+--------------+--------------+-------------+
| 1 | Jhon | 2019-01-01 | 100 |
| 2 | Daniel | 2019-01-02 | 110 |
| 3 | Jade | 2019-01-03 | 120 |
| 4 | Khaled | 2019-01-04 | 130 |
| 5 | Winston | 2019-01-05 | 110 |
| 6 | Elvis | 2019-01-06 | 140 |
| 7 | Anna | 2019-01-07 | 150 |
| 8 | Maria | 2019-01-08 | 80 |
| 9 | Jaze | 2019-01-09 | 110 |
| 1 | Jhon | 2019-01-10 | 130 |
| 3 | Jade | 2019-01-10 | 150 |
+-------------+--------------+--------------+-------------+
Result table:
+--------------+--------------+----------------+
| visited_on | amount | average_amount |
+--------------+--------------+----------------+
| 2019-01-07 | 860 | 122.86 |
| 2019-01-08 | 840 | 120 |
| 2019-01-09 | 840 | 120 |
| 2019-01-10 | 1000 | 142.86 |
+--------------+--------------+----------------+
1st moving average from 2019-01-01 to 2019-01-07 has an average_amount of (100 + 110 + 120 + 130 + 110 + 140 + 150)/7 = 122.86
2nd moving average from 2019-01-02 to 2019-01-08 has an average_amount of (110 + 120 + 130 + 110 + 140 + 150 + 80)/7 = 120
3rd moving average from 2019-01-03 to 2019-01-09 has an average_amount of (120 + 130 + 110 + 140 + 150 + 80 + 110)/7 = 120
4th moving average from 2019-01-04 to 2019-01-10 has an average_amount of (130 + 110 + 140 + 150 + 80 + 110 + 130 + 150)/7 = 142.86
Solution
Approach: Daily Totals, Then a 7-Day Window
- One row per day. Several customers can visit on the same day (two on 2019-01-10), so first sum
amountpervisited_on. A window over the raw rows would treat those two visits as two separate days. - 7-day sum.
SUM(amount) OVER (ORDER BY visited_on RANGE BETWEEN INTERVAL 6 DAY PRECEDING AND CURRENT ROW)adds up the day and the 6 calendar days before it.RANGEwith an interval counts dates, not rows, so it stays correct even if a date were missing. - Full windows only. The first six days don't have 6 days before them, so the result starts at the first date plus 6 days.
- The average is the 7-day sum divided by 7, rounded to 2 decimals. Order by
visited_on.
For 2019-01-10 the window is 2019-01-04 to 2019-01-10: 130 + 110 + 140 + 150 + 80 + 110 + 280 = 1000, and 1000 / 7 = 142.86.
The Pandas version sums per day, uses rolling("7D") on the date index for the same 7-day sum, drops the first six days and divides by 7.
WITH daily AS (
SELECT visited_on,
SUM(amount) AS amount
FROM Customer
GROUP BY visited_on
),
weekly AS (
SELECT visited_on,
-- This day and the 6 days before it.
SUM(amount) OVER (
ORDER BY visited_on
RANGE BETWEEN INTERVAL 6 DAY PRECEDING AND CURRENT ROW
) AS amount
FROM daily
)
SELECT visited_on,
amount,
ROUND(amount / 7, 2) AS average_amount
FROM weekly
-- Only days that have a full 7-day window.
WHERE visited_on >= (SELECT MIN(visited_on) FROM Customer) + INTERVAL 6 DAY
ORDER BY visited_on;
Now write it from memory
Close the write-up and solve it yourself against the tests.