Request a demo

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

FunctionWhat it does
DENSE_RANKDENSE_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_VALUEFIRST_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.
LAGLAG(value, offset, default) returns a value from an earlier row of the window partition, the previous row by default.
LAST_VALUELAST_VALUE(x) returns x from the last row of the current window frame.
LEADLEAD(value, offset, default) returns a value from a later row of the window partition, the next row by default.
NTILENTILE(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_RANKPERCENT_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_CONTPERCENTILE_CONT(x, p) OVER (...) returns the exact p-th percentile of x with linear interpolation, such as the median with p = 0.5.
PERCENTILE_DISCPERCENTILE_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.
RANKRANK() assigns 1-based ranks within a window partition; tied rows share a rank and the next rank skips ahead (1, 1, 3).
ROW_NUMBERROW_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

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.