APPROX_DISTINCT() OVER ...

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

Returns the approximate number of distinct values in the window frame.

Syntax

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

Notes

  • Returns approximate count of distinct values using HyperLogLog algorithm
  • Much faster than exact COUNT(DISTINCT) for large datasets
  • Typical error rate around 2.3%
  • Always returns BIGINT type

See also

Examples

FeatureQL
SELECT
    -- Per-row approximate distinct counts (BIGINTs), not the values in v
    f1 := ZIP(ARRAY(1, 2, 3, 4) AS id, ARRAY(30, 10, 20, 5) AS v).TRANSFORM(
        SELECT APPROX_DISTINCT(v) OVER (ORDER BY id ASC)
    ).UNWRAP(),
    -- Approximate distinct 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 APPROX_DISTINCT(v) FILTER (WHERE v > 15) OVER (ORDER BY id ASC)
    ).UNWRAP()
;
Result
f1 ARRAYf2 ARRAY
[1, 2, 3, 4][1, 1, 2, 2]

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