In MySQL, the default window frame for window functions like AVG()
is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
when no explicit frame is specified.
What This Means:
UNBOUNDED PRECEDING
: The frame starts at the first row of the partition.CURRENT ROW
: The frame ends at the current row.
This means the function operates on all rows in the partition up to and including the current row.
- For custom frames: -
OVER (PARTITION BY col_name ORDER BY col_name RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
The UNBOUNDED FOLLOWING
window frame is used in a window function to define the window as starting from the current row and extending to the last row in the partition. It's useful when you want to include all rows from the current position to the end of the partition.
- For Moving average
- `AVG(col) OVER (p by o by ROWS BETWEEN 2 PRECEDING AND CURRENT ROW): - 3 Month moving average example.