BigQuery reference
BigQuery window functions
Window (analytic) functions compute a value for every row from a window of related rows, defined with OVER (PARTITION BY ... ORDER BY ...). They rank, number, compare with neighbouring rows and compute running totals without collapsing rows.
Functions in this category
| Function | What it does |
|---|---|
| DENSE_RANK | DENSE_RANK() ranks rows within a window partition without gaps: tied rows share a rank and the next distinct value gets the next integer (1, 1, 2). |
| FIRST_VALUE | FIRST_VALUE(x) returns x from the first row of the current window frame, such as each session's landing page or a series' starting value. |
| LAG | LAG(value, offset, default) returns a value from an earlier row of the window partition, the previous row by default. |
| LAST_VALUE | LAST_VALUE(x) returns x from the last row of the current window frame. |
| LEAD | LEAD(value, offset, default) returns a value from a later row of the window partition, the next row by default. |
| NTILE | NTILE(n) splits the ordered rows of each window partition into n buckets of nearly equal size and returns the bucket number, from 1 to n. |
| PERCENT_RANK | PERCENT_RANK() returns each row's relative rank in its window partition as a FLOAT64 from 0 to 1, computed as (rank - 1) / (rows - 1). |
| PERCENTILE_CONT | PERCENTILE_CONT(x, p) OVER (...) returns the exact p-th percentile of x with linear interpolation, such as the median with p = 0.5. |
| PERCENTILE_DISC | PERCENTILE_DISC(x, p) OVER (...) returns the first value of x whose cumulative distribution reaches p, so the answer is always a value from the data. |
| RANK | RANK() assigns 1-based ranks within a window partition; tied rows share a rank and the next rank skips ahead (1, 1, 3). |
| ROW_NUMBER | ROW_NUMBER() numbers the rows of each window partition 1, 2, 3 in ORDER BY order, with no ties and no gaps. |
More of the BigQuery reference
Date functionsDatetime functionsTimestamp functionsString functionsAggregate functionsArray functionsJSON functionsConversion functionsConditional expressionsMathematical functionsHash and utility functionsQuery syntaxDDL statementsDML statementsScripting and data movementINFORMATION_SCHEMAError messagesBigQuery pricingPricing calculator
