Complete the code to calculate a simple moving average of sales over the last 3 rows.
SELECT sales, AVG(sales) OVER (ORDER BY date ROWS BETWEEN 2 PRECEDING AND [1]) AS moving_avg FROM sales_data;
The window frame clause uses CURRENT ROW to specify the current row as the end of the frame for the moving average.
Complete the code to calculate a moving average over the last 5 rows including the current row.
SELECT sales, AVG(sales) OVER (ORDER BY date ROWS BETWEEN 4 PRECEDING AND [1]) AS moving_avg FROM sales_data;
The frame ends at CURRENT ROW and starts 4 rows before, covering 5 rows total.
Fix the error in the window frame clause to calculate a moving average over the last 3 rows.
SELECT sales, AVG(sales) OVER (ORDER BY date ROWS BETWEEN 2 PRECEDING AND [1]) AS moving_avg FROM sales_data;
The frame should end at CURRENT ROW to include the current row and the two preceding rows.
Fill both blanks to calculate a moving average over the current row and the 2 preceding rows.
SELECT sales, AVG(sales) OVER (ORDER BY date ROWS BETWEEN [1] AND [2]) AS moving_avg FROM sales_data;
The frame starts 2 rows before and ends at the current row to cover 3 rows total.
Fill all three blanks to calculate a moving average over the current row and the 4 preceding rows, excluding the current row.
SELECT sales, AVG(sales) OVER (ORDER BY date ROWS BETWEEN [1] AND [2] EXCLUDE [3]) AS moving_avg FROM sales_data;
The frame covers 4 rows before the current row and excludes the current row itself using EXCLUDE CURRENT ROW.