Skip to content

Commit 510ad97

Browse files
committed
Update 1321. Restaurant Growth.sql
1 parent c06955e commit 510ad97

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
321. Restaurant Growth
2+
Solved
3+
Medium
4+
Topics
5+
Companies
6+
SQL Schema
7+
Pandas Schema
8+
Table: Customer
9+
10+
+---------------+---------+
11+
| Column Name | Type |
12+
+---------------+---------+
13+
| customer_id | int |
14+
| name | varchar |
15+
| visited_on | date |
16+
| amount | int |
17+
+---------------+---------+
18+
In SQL,(customer_id, visited_on) is the primary key for this table.
19+
This table contains data about customer transactions in a restaurant.
20+
visited_on is the date on which the customer with ID (customer_id) has visited the restaurant.
21+
amount is the total paid by a customer.
22+
23+
24+
You are the restaurant owner and you want to analyze a possible expansion (there will be at least one customer every day).
25+
26+
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.
27+
28+
Return the result table ordered by visited_on in ascending order.
29+
30+
The result format is in the following example.
31+
32+
33+
34+
Example 1:
35+
36+
Input:
37+
Customer table:
38+
+-------------+--------------+--------------+-------------+
39+
| customer_id | name | visited_on | amount |
40+
+-------------+--------------+--------------+-------------+
41+
| 1 | Jhon | 2019-01-01 | 100 |
42+
| 2 | Daniel | 2019-01-02 | 110 |
43+
| 3 | Jade | 2019-01-03 | 120 |
44+
| 4 | Khaled | 2019-01-04 | 130 |
45+
| 5 | Winston | 2019-01-05 | 110 |
46+
| 6 | Elvis | 2019-01-06 | 140 |
47+
| 7 | Anna | 2019-01-07 | 150 |
48+
| 8 | Maria | 2019-01-08 | 80 |
49+
| 9 | Jaze | 2019-01-09 | 110 |
50+
| 1 | Jhon | 2019-01-10 | 130 |
51+
| 3 | Jade | 2019-01-10 | 150 |
52+
+-------------+--------------+--------------+-------------+
53+
Output:
54+
+--------------+--------------+----------------+
55+
| visited_on | amount | average_amount |
56+
+--------------+--------------+----------------+
57+
| 2019-01-07 | 860 | 122.86 |
58+
| 2019-01-08 | 840 | 120 |
59+
| 2019-01-09 | 840 | 120 |
60+
| 2019-01-10 | 1000 | 142.86 |
61+
+--------------+--------------+----------------+
62+
Explanation:
63+
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
64+
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
65+
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
66+
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
67+
68+

0 commit comments

Comments
 (0)