ARRAY_HISTOGRAM()

All functions > ARRAY > ARRAY_HISTOGRAM()

Returns a frequency histogram of values in an array as an array of value-count pairs.

Signatures

Returns: Array of value-count pairs sorted by value

ARRAY_HISTOGRAM(array: ARRAY<T>) → ARRAY<ROW(value: T, count: BIGINT)>
sql
ParameterTypeRequiredDescription
arrayARRAY<T>YesThe array to create histogram from

Notes

  • Counts frequency of each distinct value in the array
  • Returns array of ROW structures with value and count fields
  • Results are sorted by value in ascending order
  • NULL values are counted separately
  • Empty input may yield NULL or an empty array depending on the execution target
  • Useful for analyzing value distributions

See also

Examples

Value counts

FeatureQL
SELECT
    f1 := ARRAY_HISTOGRAM(ARRAY[1, 1, 2, 3]) -- Distinct values with counts, sorted by value
;
Result
f1 VARCHAR
[{value: 1, count: 2}, {value: 2, count: 1}, {value: 3, count: 1}]

Edge cases

FeatureQL
SELECT
    f1 := ARRAY_HISTOGRAM(ARRAY[]::BIGINT[]) -- Empty array → NULL on DuckDB
;
Result
f1 VARCHAR
NULL

Last update at: 2026/05/26 17:22:09