Rolling 3-Month Revenue
SQL coding challenge · Difficulty: medium · +150 XP
Problem
Show a 3-month rolling revenue sum for each month.
Tables
Table: monthly_revenue
| month_start | revenue | | --- | --- | | 2024-01-01 | 1000 | | 2024-02-01 | 1500 | | 2024-03-01 | 1200 | | 2024-04-01 | 1800 | | 2024-05-01 | 1300 |
Expected Output
| month_start | revenue | rolling_3month_revenue | | --- | --- | --- | | 2024-01-01 | 1000 | 1000 | | 2024-02-01 | 1500 | 2500 | | 2024-03-01 | 1200 | 3700 | | 2024-04-01 | 1800 | 4500 | | 2024-05-01 | 1300 | 4300 |
- Return:
month_start,revenue,rolling_3month_revenue - Sort by
month_start ascending - Function to use:
SUM() OVER (ORDER BY month_start ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)