You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
SELECTSUM(c1.amount)
38
+
FROM Customer AS c1
39
+
WHEREc1.visited_on BETWEEN c.visited_on- INTERVAL '6 days'ANDc.visited_on
40
+
) AS amount,
41
+
(
42
+
SELECT ROUND(SUM(c2.amount)::NUMERIC/7, 2)
43
+
FROM Customer AS c2
44
+
WHEREc2.visited_on BETWEEN c.visited_on- INTERVAL '6 days'ANDc.visited_on
0 commit comments