Window Functions
Window functions allow you to create computed fields from a group of rows (a window) that return a result for each row, as opposed to one computed result for a set of rows, as is the case with aggregate functions. The add-in supports the following window function syntax.
Note: Window function support is an experimental feature of the add-in. This functionality extends beyond the add-in'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.
SELECT A, B, <window function> OVER (<window frame>) FROM TableName
The <window function> refers to any supported window function clause, and the <window frame> refers to one or more clauses that specifies the logic by which the window is defined.
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, <window function> OVER (PARTITION BY A ORDER BY B) From Contacts
The <window function> refers to any supported window function clause.
Window Functions
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 in 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.
SELECT Discount, Name, RANK() OVER (ORDER BY Name) AS Rank FROM Contacts
If you add a PARTITION BY clause, a separate set of ranks is calculated for each partition.
SELECT Discount, Name, RANK() OVER (PARTITION BY Discount ORDER BY Name) AS Rank FROM Contacts
DENSE_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.
SELECT Discount, Name, DENSE_RANK() OVER (PARTITION BY Discount ORDER BY Name) AS Rank FROM Contacts
If you add a PARTITION BY clause, a separate set of ranks is calculated for each partition.
SELECT Discount, Name, DENSE_RANK() OVER (PARTITION BY Discount ORDER BY Name) AS Rank FROM Contacts
NTILE()
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.The syntax of NTILE() is:
NTILE(buckets) OVER ( [PARTITION BY partition_expression, ... ] ORDER BY sort_expression [ASC | DESC], ... )The following are paramaters that NTILE() supports:
- 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 is clause that specifies the logical order of rows in each partition to which the NTILE() is applied.
If the number of rows is not divisible by the buckets, the NTILE() function returns groups of two sizes with the difference by one. The larger groups always precedes the smaller group in the order set by ORDER BY in the OVER() clause.
If the total of rows is divisible by the number of buckets, the function divides the rows evenly among buckets.
Example
The following statement creates a new table named ntile_demo that stores 10 integers:CREATE TABLE sales.ntile_demo ( v INT NOT NULL ); INSERT INTO sales.ntile_demo(v) VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9),(10); SELECT * FROM sales.ntile_demo;This statement uses the NTILE() function to divide ten rows into three groups:
SELECT v, NTILE (3) OVER ( ORDER BY v ) buckets FROM sales.ntile_demo;
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.The syntax of PERCENT_RANK() is:
PERCENT_RANK() OVER ( [PARTITION BY partition_expression, ... ] ORDER BY sort_expression [ASC | DESC], ... )This syntax uses the following parameters.
- 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.