PERCENT_RANK() OVER ...
All functions > WINDOW FUNCTION > PERCENT_RANK() OVER ...
Returns the relative rank of the current row: (rank - 1) / (total rows - 1).
Syntax
PERCENT_RANK() [WITHIN (WHERE condition)] OVER ([PARTITION BY expr [, ...]] [ORDER BY sort_item [, ...]] [ROWS|RANGE|GROUPS frame])
Notes
- Returns a value between 0 and 1 inclusive
- First row in each partition always has PERCENT_RANK of 0
- Requires ORDER BY clause to determine ranking order
- PARTITION BY creates independent ranking groups
See also
Examples
FeatureQL
SELECT
-- Relative rank in [0, 1]
f1 := ZIP(ARRAY(1, 2, 3) AS id, ARRAY(10, 20, 30) AS v).TRANSFORM(
SELECT PERCENT_RANK() OVER (ORDER BY v ASC).ROUND(2)
).UNWRAP(),
-- Relative ranks over matching rows only; rows outside WITHIN return NULL
f2 := ZIP(
ARRAY(1, 2, 3, 4) AS id,
ARRAY(20, 10, 20, 40) AS s,
ARRAY(TRUE, FALSE, TRUE, TRUE) AS keep
).TRANSFORM(
SELECT PERCENT_RANK() WITHIN (WHERE keep) OVER (ORDER BY s ASC).ROUND(2)
).UNWRAP()
;Result
| f1 ARRAY | f2 ARRAY |
|---|---|
| [0.00, 0.50, 1.00] | [0.00, null, 0.00, 1.00] |