MIN_BY() OVER ...

All functions > WINDOW FUNCTION > MIN_BY() OVER ...

Returns the value of the first expression corresponding to the minimum value of the second expression in the window frame.

Syntax

MIN_BY(expr, key) [FILTER (WHERE condition) | WITHIN (WHERE condition)] OVER ([PARTITION BY expr [, ...]] [ORDER BY sort_item [, ...]] [ROWS|RANGE|GROUPS frame])

Notes

  • Returns value_expression for the row with minimum key_expression
  • Useful for finding associated values with minimum keys
  • Returns same type as first expression
  • NULL values in key are ignored

See also

Examples

FeatureQL
SELECT
    -- Label at minimum key seen so far
    f1 := ZIP(
        ARRAY(1, 2, 3, 4) AS id,
        ARRAY(30, 10, 20, 5) AS v,
        ARRAY('c', 'a', 'b', 'd') AS label,
        ARRAY(3, 1, 2, 4) AS k
    ).TRANSFORM(SELECT MIN_BY(label, k) OVER (ORDER BY id ASC)).UNWRAP(),
    -- MIN_BY 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,
        ARRAY('c', 'a', 'b', 'd') AS label,
        ARRAY(3, 1, 2, 4) AS k
    ).TRANSFORM(
        SELECT MIN_BY(label, k) FILTER (WHERE v > 15) OVER (ORDER BY id ASC)
    ).UNWRAP()
;
Result
f1 ARRAYf2 ARRAY
[c, a, a, a][c, c, b, b]

Last update at: 2026/06/20 10:08:10