-
AVG(field) — assigns the average of the values in the field column for rows within the specified window frame to all rows in that window frame. For example, AVG(Salary) OVER (PARTITION BY Department) FROM Company.Employee could be used to compare each employee’s salary against the average of the salaries earned by employees within that employee’s department. AVG() supports the ROWS clause. Refer to the AVG() function’s reference page for further details on use.
-
COUNT( * | field ) — assigns a number to each row in the specified window frame starting at 1. If field is specified, the count is only incremented if the contents of field is non-null. Otherwise, the count is incremented with every row.
-
CUME_DIST() — assigns the cumulative distribution value for all rows within the specified window frame. The cumulative distribution is calculated by counting the rows with values less than or equal to the current row’s value and dividing that count by the total number of rows in the window. The column name that the cumulative distribution is computed on is specified in the ORDER BY clause.
-
DENSE_RANK() — assigns a ranking integer to each row within the same window frame, starting with 1. The ranking integers are always consecutive, unlike with the RANK() window function. Ranking integers can include duplicates values if multiple rows contain the same value for the window function field.
-
FIRST_VALUE(field) — assigns the value of the field column for the first row (ROW_NUMBER()=1) within the specified window frame to all rows in that window frame. For example, FIRST_VALUE(Country) OVER (PARTITION BY City). FIRST_VALUE() supports the ROWS clause. Note that NULL collates before all values, so if the value of field in the first row is NULL, all of the rows in the window will be NULL.
-
LAST_VALUE(field) — assigns the value of the field column for the last row within the specified window frame to all rows in that window frame. LAST_VALUE() supports the ROWS clause.
-
LAG(field[, offset[, default]]) — assigns the value of the field column for the row that is offset rows before the given row within the specified window frame. If no offset is specified, the function assigns the value of the field column 1 row before the given row by default. By default, LAG() will assign the value NULL if the given row does not have a row offset rows before it within its window frame. The user has the option to assign an alternate value under these conditions by including a value default.
-
LEAD(field[, offset[, default]]) — assigns the value of the field column for the row that is offset rows after the given row within the specified window frame. If no offset is specified, the function assigns the value of the field column 1 row after the given row by default. By default, LEAD() will assign the value NULL if the given row does not have a row offset rows after it within its window frame. The user has the option to assign an alternate value under these conditions by including a value default.
-
MAX(field) — assigns the maximum value of the field column within the specified window frame to all rows in that window frame. For example, MAX(Salary) OVER (PARTITION BY Department) FROM Company.Employee could be used to compare each employee’s salary against the highest salary earned by an employee within that employee’s department. MAX() supports the ROWS clause. Refer to the MAX() function’s reference page for further details on use.
-
MIN(field) — assigns the minimum value of the field column within the specified window frame to all rows in that window frame. For example, MIN(Salary) OVER (PARTITION BY Department) FROM Company.Employee could be used to compare each employee’s salary against the lowest salary earned by an employee within that employee’s department. MIN() supports the ROWS clause. Refer to the MIN() function’s reference page for further details on use.
-
NTH_VALUE(field, n) — assigns the value of the field column for row number n within the specified window frame (counting from 1) to all rows in that window frame. NTH_VALUE() supports the ROWS clause.
-
NTILE(num-groups) — splits the rows within the specified window frame into a num-groups number of groups that each have a roughly equal number of elements. Each group is identified by a number, starting from 1.
-
PERCENT_RANK() — assigns a ranking percentage as a fractional number between 0 and 1 (inclusive) to each row within the same window frame. Ranking percentages can include duplicate values if multiple rows contain the same value for the window function field.
-
RANK()— assigns a ranking integer to each row within the same window frame, starting with 1. Ranking integers can include duplicate values if multiple rows contain the same value for the window function field.
-
ROW_NUMBER() — assigns a unique sequential integer to each row within the same window frame, starting with 1. If multiple rows contain the same value for the window function field, each row is assigned a unique sequential integer.
-
SUM(field) — assigns the sum of the values of the field column within the specified window frame to all rows in that window frame. SUM() supports the ROWS clause. Refer to the SUM() function’s reference page for further details on use.
The following example compares the values returned by the ORDER BY clause in these window functions:
SELECT Name,City,ROW_NUMBER() OVER (ORDER BY City) AS RowNum,
RANK() OVER (ORDER BY City) AS RankNum,
PERCENT_RANK() OVER (ORDER BY City) AS RankPct
FROM CityTable ORDER BY City
This example treats all the rows as a single partition. It orders the rows by the City value and returns the following: