OVER (...) clause. Unlike aggregate functions, they run over a partition without collapsing it, so every input row is preserved in the output. Because the window value appears alongside each row rather than reducing the result, you typically compute it inside a subquery and then filter on it in the outer query to build a Segment List. Most examples below partition by user with OVER (PARTITION BY distinct_id ORDER BY requested_at) so the window spans a single user’s timeline.
row_number
Returns a unique sequential number for each row within its partition, starting at 1, in the order defined byORDER BY. Unlike rank, tied rows still receive distinct numbers.
Syntax
- None.
- The sequential position of the row, as an integer starting at 1.
Keep only each user's most recent event
rank
Returns the rank of the current row within its partition, with gaps. Rows that tie on theORDER BY value share a rank, and the next rank skips ahead by the number of tied rows.
Syntax
- None. Ranks are computed over the
ORDER BYinsideOVER.
- The rank of the row, as an integer starting at 1.
Rank each user's events from most to least recent
dense_rank
Returns the rank of the current row within its partition, with no gaps in the ranking sequence. Rows that tie on theORDER BY value share a rank, and the next distinct value gets the immediately following rank.
Syntax
- None. The ordering that ranks are computed over comes from the
ORDER BYinsideOVER.
- The rank of the row, as an integer starting at 1.
Rank each user's events by recency, no gaps on ties
lag
Returns the value of the expression evaluated at the row a given number of positions before the current row within the partition. Rows with no such preceding row return the default (NULL if unspecified). Syntaxexpr— the expression to read from the earlier row.offset— how many rows back to look. Optional; defaults to 1.default— the value returned when the offset falls outside the partition. Optional; defaults to NULL.
- The value of
exprfrom the preceding row (ordefault); its type matchesexpr.
Time since each user's previous event, in seconds
lead
Returns the value of the expression evaluated at the row a given number of positions after the current row within the partition. Rows with no such following row return the default (NULL if unspecified). Syntaxexpr— the expression to read from the later row.offset— how many rows forward to look. Optional; defaults to 1.default— the value returned when the offset falls outside the partition. Optional; defaults to NULL.
- The value of
exprfrom the following row (ordefault); its type matchesexpr.
Each event alongside the user's next event name
first_value
Returns the value of the given expression evaluated at the first row of the window frame. Syntaxexpr— the expression whose value is taken from the first row of the frame.
- The value of
exprat the first row of the frame; its type matchesexpr.
Users whose first-ever event was a signup
last_value
Returns the value of the given expression evaluated at the last row of the window frame. Note that the default frame ends at the current row, so to read the true last row of the partition you must widen the frame withROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.
Syntax
expr— the expression whose value is taken from the last row of the frame.
- The value of
exprat the last row of the frame; its type matchesexpr.
Users whose most recent event was a cancellation
count(*) over ()
Used as a window function,count(*) counts rows across the window instead of collapsing them into one row. With an empty OVER () it returns the grand total for the whole result, with PARTITION BY it returns a per-partition total, and adding ORDER BY turns it into a running count.
Syntax
- None. The window is defined entirely by the
OVERclause.
- The count of rows in the window frame, as an integer.
Keep users with more than 100 total events
ntile
Divides the rows of each partition into a given number of ranked buckets as evenly as possible, and returns the bucket number the current row falls into. When the row count does not divide evenly, earlier buckets receive one extra row. Syntaxnum_buckets— the number of buckets to distribute rows into.
- The bucket number, as an integer from 1 to
num_buckets.
Split each user's events into activity quartiles
nth_value
Returns the value of the given expression evaluated at the nth row of the window frame (counting from 1). Returns NULL if the frame has fewer than n rows. As withlast_value, the default frame ends at the current row, so to read the nth row of the whole partition you must widen the frame with ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.
Syntax
expr— the expression whose value is taken from the nth row.n— the 1-based position within the frame to read.
- The value of
exprat the nth row of the frame, or NULL if it does not exist; its type matchesexpr.
Users whose second event was adding an item to cart
percent_rank
Returns the relative rank of the current row within its partition, computed as(rank() - 1) / (total_rows - 1). The first row is always 0 and the last is always 1; a partition with a single row returns 0.
Syntax
- None.
- The relative rank as a fraction, as a floating-point number in the range 0 through 1.
Events in the earliest 25% of each user's timeline
cume_dist
Returns the cumulative distribution of the current row within its partition: the number of rows ordered at or before the current row, divided by the total number of rows in the partition. Values range from just above 0 up to 1. Syntax- None.
- The relative position of the row as a fraction, as a floating-point number in the range greater than 0 through 1.
Events in the final 10% of each user's timeline
Window frames (ROWS BETWEEN)
A frame clause restricts an aggregate window function to a sliding subset of rows relative to the current row, letting you compute running totals and moving windows.ROWS BETWEEN <start> AND <end> counts a physical number of rows, where each bound is UNBOUNDED PRECEDING, N PRECEDING, CURRENT ROW, N FOLLOWING, or UNBOUNDED FOLLOWING. Any aggregate (such as count, sum, or avg) can be paired with a frame.
Syntax
<start>— the first row of the frame, relative to the current row.<end>— the last row of the frame, relative to the current row.
- The aggregate computed over the rows in the frame; its type matches the aggregate used.
Rolling count of each user's last 3 events, current row included