Skip to content

Commit f2196e6

Browse files
committed
task: #1321
1 parent 6408323 commit f2196e6

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Have a good contributing!
9292
- [1174. Immediate Food Delivery II](./leetcode/medium/1174.%20Immediate%20Food%20Delivery%20II.sql)
9393
- [1193. Monthly Transactions I](./leetcode/medium/1193.%20Monthly%20Transactions%20I.sql)
9494
- [1204. Last Person to Fit in the Bus](./leetcode/medium/1204.%20Last%20Person%20to%20Fit%20in%20the%20Bus.sql)
95+
- [1321. Restaurant Growth](./leetcode/medium/1321.%20Restaurant%20Growth.sql)
9596
- [1341. Movie Rating](./leetcode/medium/1341.%20Movie%20Rating.sql)
9697
- [1907. Count Salary Categories](./leetcode/medium/1907.%20Count%20Salary%20Categories.sql)
9798
- [1934. Confirmation Rate](./leetcode/medium/1934.%20Confirmation%20Rate.sql)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Question 1321. Restaurant Growth
3+
Link: https://leetcode.com/problems/restaurant-growth/description/?envType=study-plan-v2&envId=top-sql-50
4+
5+
Table: Customer
6+
7+
+---------------+---------+
8+
| Column Name | Type |
9+
+---------------+---------+
10+
| customer_id | int |
11+
| name | varchar |
12+
| visited_on | date |
13+
| amount | int |
14+
+---------------+---------+
15+
In SQL,(customer_id, visited_on) is the primary key for this table.
16+
This table contains data about customer transactions in a restaurant.
17+
visited_on is the date on which the customer with ID (customer_id) has visited the restaurant.
18+
amount is the total paid by a customer.
19+
20+
21+
You are the restaurant owner and you want to analyze a possible expansion (there will be at least one customer every day).
22+
23+
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.
24+
25+
Return the result table ordered by visited_on in ascending order.
26+
*/
27+
28+
WITH visited_dates AS (
29+
SELECT visited_on + INTERVAL '6 days' AS dates
30+
FROM Customer
31+
GROUP BY visited_on
32+
)
33+
34+
SELECT DISTINCT
35+
c.visited_on,
36+
(
37+
SELECT SUM(c1.amount)
38+
FROM Customer AS c1
39+
WHERE c1.visited_on BETWEEN c.visited_on - INTERVAL '6 days' AND c.visited_on
40+
) AS amount,
41+
(
42+
SELECT ROUND(SUM(c2.amount)::NUMERIC / 7, 2)
43+
FROM Customer AS c2
44+
WHERE c2.visited_on BETWEEN c.visited_on - INTERVAL '6 days' AND c.visited_on
45+
) AS average_amount
46+
FROM Customer AS c
47+
LEFT JOIN
48+
visited_dates AS v
49+
ON c.visited_on = v.dates
50+
WHERE v.dates IS NOT NULL

0 commit comments

Comments
 (0)