Window function support is an experimental feature of the driver. This functionality extends beyond the driver’s core scope of being SQL-92 compliant. As such, performance with window functions may not be optimal.
Window Function Clauses
OVER
The OVER clause defines the window over which window functions are performed.PARTITION BY
The PARTITION BY clause subdivides a window into sub-windows called partitions. For each unique value in the column specified in the PARTITION BY clause, every record with that value collectively forms an individual partition. SELECT A, B, OVER (PARTITION BY A ORDER BY B) From Lead The refers to any supported window function clause.Math Functions
These window functions perform mathematical operations on the records within the window.COUNT
Calculates the number of records in each partition. The calculated column is of the data type “int”. SyntaxCOUNT_BIG
Calculates the number of records in each partition. The calculated column is of the data type “bigint”. SyntaxMIN
Calculates the minimum value of a numerical column per partition. SyntaxMAX
Calculates the maximum value of a numerical column per partition. SyntaxSUM
Calculates the sum of a numerical column per partition. SyntaxAVG
Calculates the average value of a numerical column per partition. SyntaxMEDIAN
Calculates the median value of a numerical column per partition. SyntaxSTDEV
Calculates the standard deviation of a numerical column per partition. SyntaxSTDEVP
Calculates the population standard deviation of a numerical column per partition. SyntaxVAR
Calculates the statistical standard variance of a numerical column per partition. SyntaxVARP
Calculates the variance population of a numerical column per partition. SyntaxRanking Functions
These window functions rank records that fall within the window and its partitions.RANK
Assigns a rank number to each record in a window based on the value of the column specified in the required ORDER BY clause. If two or more records have an equal value in the ranked column, they all receive the same rank number and the rank count increments internally, skipping ahead one rank number for each record with a duplicate value in the ORDER BY column. SyntaxDENSE_RANK
Operates like the RANK() function, but it doesn’t increment the internal rank counter for each record with a duplicate value in the ranked column. This means that, while records with identical values in the ORDER BY column still share a rank number, the function never skips a rank number. SyntaxROW_NUMBER
Calculates a row number for each record. An ORDER BY clause in the OVER clause is required. SyntaxNTILE
Distributes rows of an ordered partition into a specified number of approximately equal groups, or buckets. It assigns each group a bucket number starting from one. For each row in a group, the NTILE() function assigns a bucket number representing the group to which the row belongs. Syntax- buckets The number of buckets into which the rows are divided. The buckets can be an expression or subquery that evaluates to a positive integer. It cannot be a window function.
- PARTITION BY Distributes rows of a result set into partitions to which the NTILE() function is applied.
- ORDER BY Clause that specifies the logical order of rows in each partition to which the NTILE() is applied.
Analytical Functions
These window functions perform analytical operations on the records within the window.PERCENT_RANK
Calculates the relative rank SQL Percentile of each row. It returns values greater than zero, but the maximum value is one. It does not count any NULL values. This function is nondeterministic. Syntax- PARTITION BY By default, SQL Server treats the whole data set as a single set. You can specify the PARTITION BY clause to divide data into multiple sets. The Percent_Rank function performs the analytical calculations on each set. This parameter is optional.
- ORDER BY Sorts the data in either ascending or descending order. This parameter is required.