7-Day Rolling Sales by Store
SQL coding challenge · Difficulty: medium · +150 XP
Problem
For each row show a 7-day (7-row) rolling sum of sales per store.
Tables
Table: store_sales
| store_id | sale_date | sales | | --- | --- | --- | | 1 | 2024-01-01 | 100 | | 1 | 2024-01-02 | 150 | | 1 | 2024-01-03 | 200 | | 1 | 2024-01-04 | 120 | | 1 | 2024-01-05 | 180 | | 1 | 2024-01-06 | 90 | | 1 | 2024-01-07 | 210 | | 1 | 2024-01-08 | 60 | | 2 | 2024-01-01 | 300 | | 2 | 2024-01-02 | 250 | | 2 | 2024-01-03 | 100 |
Expected Output
| store_id | sale_date | sales | rolling_7day_sum | | --- | --- | --- | --- | | 1 | 2024-01-01 | 100 | 100 | | 1 | 2024-01-02 | 150 | 250 | | 1 | 2024-01-03 | 200 | 450 | | 1 | 2024-01-04 | 120 | 570 | | 1 | 2024-01-05 | 180 | 750 | | 1 | 2024-01-06 | 90 | 840 | | 1 | 2024-01-07 | 210 | 1050 | | 1 | 2024-01-08 | 60 | 1010 | | 2 | 2024-01-01 | 300 | 300 | | 2 | 2024-01-02 | 250 | 550 | | 2 | 2024-01-03 | 100 | 650 |
- Return:
store_id,sale_date,sales,rolling_7day_sum - Sort by
store_id, sale_date - Function to use:
SUM() OVER (PARTITION BY store_id ORDER BY sale_date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW)