-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01321.sql
More file actions
30 lines (30 loc) · 763 Bytes
/
Copy path01321.sql
File metadata and controls
30 lines (30 loc) · 763 Bytes
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
-- Write your PostgreSQL query statement below
WITH DailySales AS (
SELECT
visited_on,
SUM(amount) AS daily_amount
FROM Customer
GROUP BY visited_on
),
MovingMetrics AS (
SELECT
visited_on,
SUM(daily_amount) OVER(
ORDER BY visited_on
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW
) AS amount,
ROUND(
AVG(daily_amount) OVER(
ORDER BY visited_on
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW
), 2
) AS average_amount
FROM DailySales
)
SELECT
visited_on,
amount,
average_amount
FROM MovingMetrics
WHERE visited_on >= (SELECT MIN(visited_on) FROM Customer) + INTERVAL '6 days'
ORDER BY visited_on ASC;