Skip to content

Commit 0328e83

Browse files
authored
Solved -> 1321. Restaurant Growth
1 parent ff5dfb8 commit 0328e83

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
3+
Problem Link -> https://leetcode.com/problems/restaurant-growth/description/?envType=study-plan-v2&envId=top-sql-50
4+
5+
------------------------------------------------------------- QUESTION -----------------------------------------------------------
6+
7+
Table: Customer
8+
9+
+---------------+---------+
10+
| Column Name | Type |
11+
+---------------+---------+
12+
| customer_id | int |
13+
| name | varchar |
14+
| visited_on | date |
15+
| amount | int |
16+
+---------------+---------+
17+
In SQL,(customer_id, visited_on) is the primary key for this table.
18+
This table contains data about customer transactions in a restaurant.
19+
visited_on is the date on which the customer with ID (customer_id) has visited the restaurant.
20+
amount is the total paid by a customer.
21+
22+
23+
You are the restaurant owner and you want to analyze a possible expansion (there will be at least one customer every day).
24+
Compute the moving average of how much the customer paid in a seven days window (i.e., current day + 6 days before). average_amount should be rounded to two decimal places.
25+
Return the result table ordered by visited_on in ascending order.
26+
The result format is in the following example.
27+
28+
29+
Example 1:
30+
31+
Input:
32+
Customer table:
33+
+-------------+--------------+--------------+-------------+
34+
| customer_id | name | visited_on | amount |
35+
+-------------+--------------+--------------+-------------+
36+
| 1 | Jhon | 2019-01-01 | 100 |
37+
| 2 | Daniel | 2019-01-02 | 110 |
38+
| 3 | Jade | 2019-01-03 | 120 |
39+
| 4 | Khaled | 2019-01-04 | 130 |
40+
| 5 | Winston | 2019-01-05 | 110 |
41+
| 6 | Elvis | 2019-01-06 | 140 |
42+
| 7 | Anna | 2019-01-07 | 150 |
43+
| 8 | Maria | 2019-01-08 | 80 |
44+
| 9 | Jaze | 2019-01-09 | 110 |
45+
| 1 | Jhon | 2019-01-10 | 130 |
46+
| 3 | Jade | 2019-01-10 | 150 |
47+
+-------------+--------------+--------------+-------------+
48+
49+
Output:
50+
+--------------+--------------+----------------+
51+
| visited_on | amount | average_amount |
52+
+--------------+--------------+----------------+
53+
| 2019-01-07 | 860 | 122.86 |
54+
| 2019-01-08 | 840 | 120 |
55+
| 2019-01-09 | 840 | 120 |
56+
| 2019-01-10 | 1000 | 142.86 |
57+
+--------------+--------------+----------------+
58+
59+
Explanation:
60+
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
61+
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
62+
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
63+
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
64+
65+
*/
66+
67+
-- ----------------------------------------------------------- SOLUTION -----------------------------------------------------------
68+
69+
SELECT
70+
visited_on,
71+
(
72+
SELECT SUM(amount)
73+
FROM Customer
74+
WHERE visited_on BETWEEN DATE_SUB(c.visited_on, INTERVAL 6 DAY) AND c.visited_on
75+
) AS amount,
76+
ROUND((
77+
SELECT SUM(amount)/7
78+
FROM Customer
79+
WHERE visited_on BETWEEN DATE_SUB(c.visited_on, INTERVAL 6 DAY) AND c.visited_on
80+
),2) AS average_amount
81+
FROM Customer c
82+
WHERE visited_on >= (
83+
SELECT DATE_ADD(MIN(visited_on), INTERVAL 6 DAY)
84+
FROM Customer
85+
)
86+
GROUP BY visited_on
87+
ORDER BY visited_on;
88+

0 commit comments

Comments
 (0)