Overview of Window Functions
Window functions are a powerful analytical tool in SQL that allow you to perform calculations over a set of related rows, rather than just a single row. Window functions create a "window" to view the current row and its context rows, enabling complex data analysis tasks.
Syntax
function_name: Built-in window functions, such asSUM,COUNT, etc.expr: Function parameters, determined based on actual data.window_definition: Window definition, used to specify how to apply the window function on the dataset.
Window Definition
PARTITION BYclause: Optional, used to divide the dataset into different partitions.ORDER BYclause: Optional, used to specify the sorting method of data within each window. It is recommended to use unique columns or column combinations to reduce randomness.FRAMEclause: Optional, used to determine the boundaries of the window. Detailed description is as follows.
FRAME clause
FRAME clause defines a closed interval to determine the data boundaries. ROWS and RANGE are two types of boundary types.
ROWStype: Determines the boundary by row number.RANGEtype: WhenORDER BYis specified, the boundary is determined by the size relationship of the column values. WhenORDER BYis not specified, all rows are considered to have the same value.
frame_start and frame_end represent the start and end boundaries of the window. frame_start is required, frame_end is optional, and the default value is CURRENT ROW. The specific options are as follows:
UNBOUNDED PRECEDING: Indicates the start of the partition or result set.OFFSET PRECEDING: Indicates an offset relative to the current row.CURRENT ROW: Indicates only the current row.OFFSET FOLLOWING: Indicates an offset after the current row.UNBOUNDED FOLLOWING: Indicates the end of the partition or result set.
When the FRAME clause is not explicitly set, the default FRAME clause is:
Usage Example
Window Function Classification
Window functions can be classified into the following three categories based on their behavior:
- Ranking Functions (
ranking_function): Used to calculate rankings, must specifyORDER BY, and cannot specify theFRAMEclause. For example:ROW_NUMBERRANKDENSE_RANKPERCENT_RANKNTILE
- Analytic Functions (
analytic_function): Used to perform more complex analytical calculations. For example:CUME_DIST- `
