# Window Functions Window functions allow you to work with a subset of rows related to the currently selected row. For a given dimension, you can find the most associated dimension by some other measure (for example, number of records or sum of revenue). Window functions must always contain an OVER clause. The OVER clause splits up the rows of the query for processing by the window function. The PARTITION BY list divides the rows into groups that share the same values of the PARTITION BY expression(s). For each row, the window function is computed using all rows in the same partition as the current row. Rows that have the same value in the ORDER BY clause are considered peers. The ranking functions give the same answer for any two peer rows. ## Supported Window Functions
| Function | Description |
|---|---|
| Function | Description |
BACKWARD_FILL(value) | Replace the null value by using the nearest non-null value of the value column, using backward search. For example, for column
At least one ordering column must be defined in the window clause.
|
CONDITIONAL_CHANGE_EVENT(expr) | For each partition, a zero-initialized counter is incremented every time the result of expr changes as the expression is evaluated over the partition. Requires an ORDER BY clause for the window. |
COUNT_IF(condition_expr) | Aggregate function that can be used as a window function for both a nonframed window partition and a window frame. Returns the number of rows satisfying the given condition_expr, which must evaluate to a Boolean value (TRUE/FALSE) like x IS NULL or x > 1. |
CUME_DIST() | Cumulative distribution value of the current row: (number of rows preceding or peers of the current row)/(total rows). Window framing is ignored. |
DENSE_RANK() | Rank of the current row without gaps. This function counts peer groups. Window framing is ignored. |
FIRST_VALUE(value) | Returns the value from the first row of the window frame (the rows from the start of the partition to the last peer of the current row). |
FORWARD_FILL(value) | Replace the null value by using the nearest non-null value of the value column, using forward search. NULLS FIRST ordering of the input value is added automatically for any user-defined ordering of the input value. For example: |
LAG(value, offset) | Returns the value at the row that is offset rows before the current row within the partition. LAG_IN_FRAME is the window-frame-aware version. |
LAST_VALUE(value) | Returns the value from the last row of the window frame. |
LEAD(value, offset) | Returns the value at the row that is offset rows after the current row within the partition. LEAD_IN_FRAME is the window-frame-aware version. |
NTH_VALUE(expr,N) | Returns a value of expr at row N of the window partition. |
NTILE(num_buckets) | Subdivide the partition into buckets. If the total number of rows is divisible by num_buckets, each bucket has a equal number of rows. If the total is not divisible by num_buckets, the function returns groups of two sizes with a difference of 1. Window framing is ignored. |
PERCENT_RANK() | Relative rank of the current row: (rank-1)/(total rows-1). Window framing is ignored. |
RANK() | Rank of the current row with gaps. Equal to the row_number of its first peer. |
ROW_NUMBER() | Number of the current row within the partition, counting from 1. Window framing is ignored. |
SUM_IF(condition_expr) | Aggregate function that can be used as a window function for both a nonframed window partition and a window frame. Returns the sum of all expression values satisfying the given condition_expr. Applies to numeric data types. |
LEAD\_IN\_FRAME(value, offset)
LAG\_IN\_FRAME(value, offset)
FIRST\_VALUE\_IN\_FRAME
LAST\_VALUE\_IN\_FRAME
NTH\_VALUE\_IN\_FRAME
These are window-frame-aware versions of the LEAD, LAG , FIRST\_VALUE, LAST\_VALUE, and NTH\_VALUE functions.