Window Frame
Window Frame is a subset defined when using window functions to limit the computation scope of the window function, performing calculations only on the data within the window frame. Through window frames, you can precisely control how window functions compute and their results. Below is a detailed description and usage examples of window frames.
Window Frame Syntax
When using window functions, define the window frame through the OVER clause with the following syntax:
Where <window_function> represents the window function to use, such as ROW_NUMBER(), RANK(), SUM(), etc. The PARTITION BY clause is used to group data, with each group called a partition. The ORDER BY clause is used to sort data within each partition, which will affect the results of certain window functions such as ROW_NUMBER(), RANK(), etc. frame_clause is used to define the scope of the window frame and comes in two forms: ROWS frame and RANGE frame.
ROWS frame
ROWS frame is a row-based window frame used to specify the number or position of rows included in the window frame. Its syntax is as follows:
<start_boundary> and <end_boundary> 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 row<offset>rows before the current row, where<offset>is a non-negative integer.<offset> FOLLOWING: The row<offset>rows 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 of rows included in the window frame. Its syntax is as follows:
<start_boundary> and <end_boundary> 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.
Window Frame Usage Examples
Below are some SQL query examples using Window Frames, along with their output results. Assume we have a table called sales containing monthly sales and profit margin data as follows:
| 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 frameto calculate the sum of sales for the current month and the two months before and after.
- Query: Use the
AVG()function andROWS frameto calculate the average profit margin for the current month and the months before and after.
RANGE frame
- Query: Use the
COUNT()function andRANGE frameto calculate the distribution of each month's sales across the year, i.e., how many months have sales within 10 of the current month's sales.
- Query: Use the
MAX()function andRANGE frameto calculate the maximum profit margin among the current month and the adjacent months.
