-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrestaurantGrowth1321.sql
More file actions
45 lines (39 loc) · 1.5 KB
/
restaurantGrowth1321.sql
File metadata and controls
45 lines (39 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
Amazon 1321. Restaurant Growth https://leetcode.com/problems/restaurant-growth/
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) has visited the restaurant.
amount is the total paid by a customer.
-----------------------------------
*/
-- Final solution beats 82%:
with day_totals as (
SELECT visited_on, sum(amount) as amount
FROM customer
GROUP BY visited_on
)
SELECT visited_on,
round(sum(amount) OVER(ORDER BY visited_on ROWS BETWEEN 6 PRECEDING AND CURRENT ROW), 2) as amount,
round(avg(amount) OVER(ORDER BY visited_on ROWS BETWEEN 6 PRECEDING AND CURRENT ROW), 2) as average_amount
FROM day_totals
GROUP BY visited_on
ORDER BY visited_on
LIMIT 99999999999 OFFSET 6
-- The above solution fixed the bottom solution because you first have to do a groupby on the days:
-- Initial start that has everything correct but last row:
SELECT visited_on,
round(sum(amount) OVER(ORDER BY visited_on ROWS BETWEEN 6 PRECEDING and current row), 2) as amount,
round(avg(amount) OVER(ORDER BY visited_on ROWS BETWEEN 6 PRECEDING and current row), 2) as average_amount
FROM customer
GROUP BY visited_on
ORDER BY visited_on
LIMIT 99999999999 OFFSET 6