Window functions operate on a set of rows and return a single value for each row from the underlying query. Like aggregate functions, window functions operate on a set of rows, but they do not reduce the number of rows returned by the query. Unlike regular aggregate functions, window functions do not group rows into a single output row. Instead, the rows retain their separate identities, and the window function produces aggregate calculations that access more than just the current row in the query result. The term window describes the set of rows on which the window function operates. A window function returns a value from the rows in a window. The database uses theDocumentation Index
Fetch the complete documentation index at: https://docs.ocient.com/llms.txt
Use this file to discover all available pages before exploring further.
ORDER BY and PARTITION BY clauses to define the scope of the window.
Syntax
Window Aggregate functions follow this syntax with window_function representing any of the functions listed in Supported Window Aggregate Functions.
SQL
| Argument | Data Type | Description |
|---|---|---|
arg1, arg2 [,...] | String | The arguments of the window function. The arguments depend on the type of window function. |
| Parameter | Data Type | Description |
|---|---|---|
part_list | String | Specifies the column by which the rows are partitioned for the window function. |
order_list | String | Specifies the column by which the rows are ordered. |
row_integer | Numeric | An integer representing the number of rows to operate from the current row. |
Window Aggregate Keywords
Window aggregate functions use these clauses and keywords to designate the parameters for the query and output.PARTITION BY
This clause defines a set of rows within a result set.PARTITION BY operates similarly to a GROUP BY clause, except it does not reduce the number of rows in the result set.
ORDER BY
Sorts the result set in ascending or descending order based on one or more specified columns. AnORDER BY clause must be included on all window functions to ensure your query results are deterministic. Some window functions require an ORDER BY clause. See the syntax for each function for specifics. For details, see ORDER BY.
ASC or DESC
Specifies whether the values in the specified column should be sorted inASC ascending or DESC descending order. ASC is the default sort order.
NULLS FIRST, NULLS LAST, or IGNORE NULLS
Specifies if the database sorts NULL rows firstNULLS FIRST, last NULLS LAST, or whether the database ignores NULLs IGNORE NULLS. The default is NULLS LAST.
ROWS or RANGE
Limit the rows within the partition by specifying start and end points within the partition. Specify a range of rows with respect to the current row by logical association usingRANGE or physical association using ROWS.
The RANGE clause logically limits the rows within a partition by specifying a range of values with respect to the value in the current row. The system defines preceding or following rows based on the ordering in the ORDER BY clause. If you do not specify this clause, the default is ascending order.
The ROWS clause limits the rows within a partition by specifying a fixed number of rows preceding or following the current row.
Specifying a window frame by the RANGE keyword with the current row CURRENT ROW keyword includes all rows with the same values as the current row in the ORDER BY clause.
ROWS and RANGE clauses can include these options.
| Keyword Clause | Description |
|---|---|
UNBOUNDED PRECEDING | Specifies that the window starts at the first row of the partition. This clause specifies a window starting point only. |
UNBOUNDED FOLLOWING | Specifies that the window ends at the last row of the partition. This clause specifies a window endpoint only. For example: RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING This window frame starts with the current row and ends with the last row of the partition. |
row_integer PRECEDING | The row_integer value specifies the number of rows or values to precede the current row. You cannot use the PRECEDING keyword in a RANGE clause. |
row_integer FOLLOWING | The row_integer value specifies the number of rows or values to follow the current row. When you specify the FOLLOWING keyword as the window starting point, the endpoint must also use a FOLLOWING clause with a row value. For example: ROWS BETWEEN 2 FOLLOWING AND 10 FOLLOWING This window frame starts with the second row following the current row and ends with the tenth row following the current row. You cannot use the FOLLOWING keyword in a RANGE clause. |
CURRENT ROW | When you specify the CURRENT ROW keyword in a ROWS clause, the window frame starts or ends at the current row. When you specify this keyword in a RANGE clause, the window frame includes all rows with equal values. For example: ROWS BETWEEN 2 PRECEDING AND CURRENT ROW This clause means that the window function operates on a frame of three rows in size, where the first two rows precede the current row (inclusive). You can specify the current row as either a starting or an end point. |
Supported Window Aggregate Functions
These functions are supported for window frames. Some examples use thesys.dummy virtual table to populate data. For details, see Generate Tables Using sys.dummy.
You cannot use the
DISTINCT keyword with window aggregation functions.CUME_DIST
Returns a positive value less than one that represents a percentage of values that are less than or equal to the current value in the group. SyntaxSQL
| Parameter | Data Type | Description |
|---|---|---|
part_list | Any | Specifies the column by which the rows are partitioned for the window function. |
order_list | Any | Specifies the column by which the rows are ordered for the window function. |
SQL
Text
DELTA
Computes the finite difference between successive values of expression under the specified ordering. This is a backward difference, meaning the delta value for a specified row is the difference between the value of that row and the previous row. The function requires two rows of input data to calculate a first-degree difference, three rows of input data to calculate a second-degree difference, and so on. However, rather than returning NULL for the first few rows, the function returns the correct values, such that using DELTA of degreen followed by DELTA of degree -n cancel each other out to return the initial result (other than floating point error accumulation). In general, using this window function over larger result sets with large degrees worsens floating-point error accumulation.
Integer degree values in the range [-3, 3] are generally stable up to approximately 5 million rows. Fractional degree values in the range [-3, 3] are generally stable up to approximately 1 million rows. The stable degree range is wider over smaller result sets and narrower over larger result sets. For example, fractional and integer degrees are stable over the range [-4, 4] up to approximately 80,000 rows.
Syntax
SQL
| Argument | Data Type | Description |
|---|---|---|
expression | Numeric | The column or expression to use for the DELTA calculation. |
degree | Numeric | Optional. If degree is set to a positive integer greater than 1, the window function calculates the higher-order finite difference without using nested DELTA calls. If negative, the calculation is an anti-difference (i.e., discrete integration). You can specify fractional values, either positive or negative, and these values mean fractional differences or sums. The default is 1. |
| Parameter | Data Type | Description |
|---|---|---|
part_list | Any | Specifies the column by which the rows are partitioned for the window function. |
order_list | Any | Specifies the column by which the rows are ordered for the window function. |
SQL
Text
DELTA function with a table of regional sales data to find the difference in sales by region and year.
SQL
Text
DENSE_RANK
Assigns a rank to each row in the result set with equal values having the same number. There are no gaps between ranks. SyntaxSQL
| Parameter | Data Type | Description |
|---|---|---|
part_list | Any | Specifies the column by which the rows are partitioned for the window function. |
order_list | Any | Specifies the column by which the rows are ordered for the window function. |
SQL
Text
DERIVATIVE
The function calculates the difference between successive values of two numeric expressions, representing a backward difference (e.g., the difference between the current and previous row). It can be considered as computing the derivative of one expression with respect to another. The function must use anORDER BY clause, and it cannot use a PARTITION BY clause.
DERIVATIVE requires two arguments, expression and expression2, which must be included in the ORDER BY clause.
The function computes the difference in expression values between consecutive rows and divides this by the difference in expression2 values over the same rows. If expression represents some measured value and expression2 is time, the result is the rate of change (derivative) of the value with respect to time.
The function can compute higher-degree derivatives (second, third, etc.). To calculate the nth-degree derivative, it uses a backward difference formula that compares the current row to the previous n rows.
For example:
- A second-degree derivative requires three rows of data: the current row, the previous row, and the one before the previous row.
- A third-degree derivative would require four rows, and so on.
DERIVATIVE function is designed to return correct values even for initial rows. This behavior ensures that if you apply a derivative of degree n followed by its inverse (derivative of degree -n), you recover the original values with minimal error (except for floating point inaccuracies).
In general, using this function over larger result sets with larger degrees worsens the floating-point error. Integer degree values in the range of three are generally stable up to approximately 5 million rows. Fractional degree values in the range of three are generally stable up to approximately 1 million rows. The stable degree range is wider over smaller result sets and narrower over larger result sets. For example, fractional and integer degrees are stable over the range [-4, 4] up to approximately 80,000 rows.
Syntax
SQL
| Argument | Data Type | Description |
|---|---|---|
expression | Numeric | The column or expression to use for the DERIVATIVE calculation compared to expression2. |
expression2 | Numeric | The column or expression to use for the DERIVATIVE calculation compared to expression. |
degree | Numeric | Optional. If you set degree to a positive integer greater than one, the window function calculates the higher-order difference quotients (discrete derivatives) without using nested DERIVATIVE executions. If the degree argument is a negative integer, the calculation is a discrete anti-differentiation or integration. The default value is 1. |
x and y.
SQL
Text
SQL
Text
FIRST_VALUE
Returns the first value in the ordered result set. SyntaxSQL
| Argument | Data Type | Description |
|---|---|---|
expression | Any | The column or expression value to use for the FIRST_VALUE calculation. |
| Parameter | Data Type | Description |
|---|---|---|
part_list | Any | Specifies the column by which the rows are partitioned for the window function. |
order_list | Any | Specifies the column by which the rows are ordered for the window function. |
SQL
Text
LAG
Returns a row backward from the current row (one row back unless you specify a different number value).For marking repeated LAG calls on one or more columns, see Data Preparation.
SQL
| Argument | Data Type | Description |
|---|---|---|
value_expr | Any | The column or expression to use for the LAG calculation. |
offset | Numeric | Optional. This number determines how many rows back from the current row to return a value. The default value is 1. |
| Parameter | Data Type | Description |
|---|---|---|
part_list | Any | Specifies the column by which the rows are partitioned for the window function. |
order_list | Any | Specifies the column by which the rows are ordered for the window function. |
LAG function returns the last value for the specified column y.
SQL
Text
5 changes the number of rows back from the current row from which to return a value.
SQL
Text
LAST_VALUE
Returns the last value in the ordered result set. SyntaxSQL
| Argument | Data Type | Description |
|---|---|---|
expression | Any | The column or expression value to use for the LAST_VALUE calculation. |
| Parameter | Data Type | Description |
|---|---|---|
part_list | Any | Specifies the column by which the rows are partitioned for the window function. |
order_list | Any | Specifies the column by which the rows are ordered for the window function. |
SQL
Text
LEAD
Returns a row forward from the current row (one row forward unless you specify a different number value). SyntaxSQL
| Argument | Data Type | Description |
|---|---|---|
value_expr | Any | The value to use for the LEAD calculation. |
offset | Numeric | Optional. This number determines how many rows forward from the current row to return a value. The default value is 1. |
| Parameter | Data Type | Description |
|---|---|---|
part_list | Any | Specifies the column by which the rows are partitioned for the window function. |
order_list | Any | Specifies the column by which the rows are ordered for the window function. |
LEAD function returns the last value for the specified column y.
SQL
Text
5 in the LEAD function to change the number of rows forward from the current row to return a value.
SQL
Text
NTH_VALUE
Returns then-th value in the ordered result set.
Syntax
SQL
| Argument | Data Type | Description |
|---|---|---|
expr | Any | The column or expression from which to retrieve a value. |
offset | Numeric | A positive integer that indicates the row number, starting from the first row in the window. This integer must be positive. |
| Parameter | Data Type | Description |
|---|---|---|
part_list | Any | Specifies the column by which the rows are partitioned for the window function. |
order_list | Any | Specifies the column by which the rows are ordered for the window function. |
NTH_VALUE function returns the fifth value for the specified column y.
For the first four rows, the window contains fewer than five rows, so the function returns NULL because the fifth value is not yet available.
SQL
Text
SQL
Text
PERCENTILE
Returns the value corresponding to the specified percentile (0 ≤n ≤ 1) within the group.
The set of values is treated as a continuous distribution, so it is possible that the computed value cannot appear in the result set.
Syntax
SQL
| Argument | Data Type | Description |
|---|---|---|
expr | Any | The column or expression to use for the PERCENTILE calculation. |
percentile | Float | A percentile (0 ≤ n ≤ 1) to determine the value to return by its position within the ordering of expr. |
| Parameter | Data Type | Description |
|---|---|---|
part_list | Any | Specifies the column by which the rows are partitioned for the window function. |
order_list | Any | Specifies the column by which the rows are ordered for the window function. |
9.1 because the window column starts at 1. It would be 9 if the column started at 0.
SQL
Text
PERCENT_RANK
Returns a percentage value for each row representing the percentage of values less than the current group, excluding the highest value. The highest value in a group is always 1. SyntaxSQL
| Parameter | Data Type | Description |
|---|---|---|
part_list | Any | Specifies the column by which the rows are partitioned for the window function. |
order_list | Any | Specifies the column by which the rows are ordered for the window function. |
SQL
Text
RANK
Assigns a rank number to each row in the result set with equal values having the same number. There can be gaps between ranks. Results are similar to the ranking used in sporting events. SyntaxSQL
| Parameter | Data Type | Description |
|---|---|---|
part_list | Any | Specifies the column by which the rows are partitioned for the window function. |
order_list | Any | Specifies the column by which the rows are ordered for the window function. |
SQL
Text
RATIO_TO_REPORT
Computes the ratio of a value to the sum of the set of values. For example, theRATIO_TO_REPORT function calculates the ratio of the salary of an employee relative to the total salaries in the department where the employee belongs.
Syntax
SQL
| Argument | Data Type | Description |
|---|---|---|
ratio_expression | Numeric | A column or expression to calculate the ratio for each row compared to the total. |
| Parameter | Data Type | Description |
|---|---|---|
part_list | Any | Specifies the column by which the rows are partitioned for the window function. |
order_list | Any | Specifies the column by which the rows are ordered for the window function. |
SQL
Text
ROW_NUMBER
Assigns a unique number to each row in the result set. SyntaxSQL
| Parameter | Data Type | Description |
|---|---|---|
part_list | Any | Specifies the column by which the rows are partitioned for the window function. |
order_list | Any | Specifies the column by which the rows are ordered for the window function. |
SQL
Text
ROW_NUMBER function orders the sales team members by their region and sales amounts.
SQL
Text
ZSCORE
Calculates the z-score of the sample based on the standard deviation (similar to the STDDEV aggregate function). The standard deviation assumes that the data is a sample of a larger set, meaning it corrects for bias. If your data set is comprehensive and represents an entire group, use the ZSCOREP function instead. SyntaxSQL
| Argument | Data Type | Description |
|---|---|---|
expr | Numeric | A column or expression to calculate the standard deviation for each row. |
| Parameter | Data Type | Description |
|---|---|---|
part_list | Any | Specifies the column by which the rows are partitioned for the window function. |
order_list | Any | Specifies the column by which the rows are ordered for the window function. |
SQL
Text
ZSCOREP
Calculates the z-score of the sample based on the population standard deviation (similar to the STDEVP aggregate function). The population standard deviation assumes that the data contains the entire population, meaning there is no correction for bias. Use it when your data set is comprehensive and represents the entire group (i.e. population) you are analyzing. If your data set is a sample of a larger set, use the ZSCORE function. SyntaxSQL
| Argument | Data Type | Description |
|---|---|---|
expr | Numeric | A column or expression to calculate the standard deviation for each row. |
| Parameter | Data Type | Description |
|---|---|---|
part_list | Any | Specifies the column by which the rows are partitioned for the window function. |
order_list | Any | Specifies the column by which the rows are ordered for the window function. |
SQL
Text

