Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions OFFSET-FETCH/1321_Restaurant_Growth.sql
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
SELECT visited_on,
-- Calculate the rolling sum and average amount over a 7-day window
SELECT
visited_on,

-- Compute the rolling sum of 'amount' over the past 7 days including the current day
SUM(SUM(amount)) OVER (
ORDER BY visited_on
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW
) AS amount,

-- Compute the rolling average of 'amount' over the past 7 days including the current day
CAST(
SUM(SUM(amount)) OVER (
ORDER BY visited_on
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW
)/7.0
AS DECIMAL(10,2)
) AS average_amount
) / 7.0
AS DECIMAL(10, 2)
) AS average_amount
FROM Customer
GROUP BY visited_on
ORDER BY visited_on
Expand Down