Window Frame
Window Frame is a subset defined when using window functions to limit the calculation range of the window function, only calculating data within the window frame. Through the window frame, you can more precisely control the calculation method and results of the window function. Below are detailed explanations and usage examples of the window frame.
Window Frame Syntax
When using window functions, you can define the window frame through the OVER
clause, with the following syntax:
The syntax includes:
<window_function>
: Specifies the window function name (e.g.,ROW_NUMBER(), RANK(), SUM()
)PARTITION BY
: Defines data partitioning criteria, creating distinct groups called partitionsORDER BY
: Determines the sort order within partitions, crucial for functions likeROW_NUMBER()
andRANK()
- frame_clause: Specifies window frame boundaries, available in either ROWS or RANGE format
ROWS frame
ROWS frame is a row-based window frame used to specify the number or position of rows included in the window frame. The syntax is as follows:
<start_boundary>
and <end_boundary>
are used to define the start and end boundaries of the window frame, and can be one of the following values:
UNBOUNDED PRECEDING
: Starts from the first row of the partition.UNBOUNDED FOLLOWING
: Ends at the last row of the partition.CURRENT ROW
: The current row.<offset> PRECEDING
: The<offset>
th row before the current row, where<offset>
is a non-negative integer.<offset> FOLLOWING
: The<offset>
th row after the current row, where<offset>
is a non-negative integer.
RANGE frame
RANGE frame
is a value-based window frame used to specify the range of values for the rows included in the window frame. The syntax is as follows:
<start_boundary>
and <end_boundary>
are used to define the start and end boundaries of the window frame, and can be one of the following values:
UNBOUNDED PRECEDING
: Starts from the minimum value in the partition.UNBOUNDED FOLLOWING
: Ends at the maximum value in the partition.CURRENT ROW
: The value of the current row.<value> PRECEDING
: The value of the current row minus<value>
, where<value>
is a non-negative number.<value> FOLLOWING
: The value of the current row plus<value>
, where<value>
is a non-negative number.
Example Usage of Window Frame
Below are some examples of SQL queries using window frames and their output results. Suppose we have a table named sales that contains data on monthly sales and profit margins, as shown below:
month | sales | profit |
1 | 100 | 0.1 |
2 | 120 | 0.15 |
3 | 80 | 0.05 |
4 | 150 | 0.2 |
5 | 90 | 0.1 |
6 | 110 | 0.12 |
ROWS frame
- Query: Use the
SUM()
function andROWS frame
to calculate the total sales for each month along with the sales of the previous two months and the next two months.
- Query: Use the
AVG()
function andROWS frame
to calculate the average profit margin for each month with the profit margin of the previous month and the next month.
RANGE frame
- Query: Use the
COUNT()
function andRANGE frame
to calculate the distribution of each month's sales in the annual sales, that is, how many months' sales differ from the current month's sales by no more than 10.
- Query: Use the
MAX()
function andRANGE frame
to calculate the maximum profit margin for each month compared to the previous month and the following month.