COUNT() OVER ...
All functions > WINDOW FUNCTION > COUNT() OVER ...
Returns the count of rows in the window frame.
Syntax
COUNT([DISTINCT] expr | *) [FILTER (WHERE condition) | WITHIN (WHERE condition)] OVER ([PARTITION BY expr [, ...]] [ORDER BY sort_item [, ...]] [ROWS|RANGE|GROUPS frame])
Notes
- Counts rows over a window
- NULL values in the expression are excluded from the count
- Use COUNT(DISTINCT expr) for a running count of distinct non-NULL values
- Always returns BIGINT type
- Useful for running counts and row numbering within windows
See also
Examples
FeatureQL
SELECT
-- Cumulative non-null row count (1, then 2, …), not id
f1 := ZIP(ARRAY(1, 2, 3, 4) AS id, ARRAY(30, 10, 20, 5) AS v).TRANSFORM(
SELECT COUNT(v) OVER (ORDER BY id ASC)
).UNWRAP(),
-- Cumulative count counting only rows where v > 15 (10 and 5 excluded from the frame)
f2 := ZIP(ARRAY(1, 2, 3, 4) AS id, ARRAY(30, 10, 20, 5) AS v).TRANSFORM(
SELECT COUNT(v) FILTER (WHERE v > 15) OVER (ORDER BY id ASC)
).UNWRAP(),
-- Per-row exact distinct counts (BIGINTs), not the values in v
f3 := ZIP(ARRAY(1, 2, 3, 4) AS id, ARRAY(30, 10, 20, 5) AS v).TRANSFORM(
SELECT COUNT(DISTINCT v) OVER (ORDER BY id ASC)
).UNWRAP(),
-- Distinct count counting only rows where v > 15 (10 and 5 excluded from the frame)
f4 := ZIP(ARRAY(1, 2, 3, 4) AS id, ARRAY(30, 10, 20, 5) AS v).TRANSFORM(
SELECT COUNT(DISTINCT v) FILTER (WHERE v > 15) OVER (ORDER BY id ASC)
).UNWRAP()
;Result
| f1 ARRAY | f2 ARRAY | f3 ARRAY | f4 ARRAY |
|---|---|---|---|
| [1, 2, 3, 4] | [1, 1, 2, 2] | [1, 2, 3, 4] | [1, 1, 2, 2] |