collect_list
Returns a non-NULL array. Grouped DISTINCT calls keep one NULL when present; ordinary calls skip NULL values.
Syntax
A window function keeps each input row and adds a result for that row.PARTITION BY groups rows with matching values; without it, all input rows form one group.
Aggregate
Window
Arguments
Aggregate and window
Returns
- Type: ARRAY with the SQL type of value as its element type
- NULL behavior: Returns a non-NULL array. Grouped DISTINCT calls keep one NULL when present; ordinary calls skip NULL values.
- Empty input: Returns [] on empty input. All-NULL non-DISTINCT input returns []; all-NULL grouped DISTINCT input returns [NULL].
- Shape: One-dimensional array; grouped DISTINCT calls retain one of each value.
- Ordering: Grouped aggregation returns elements in an unspecified order, even with WITHIN GROUP. Use ARRAY_SORT for a sorted grouped result. In a full-group window call, WITHIN GROUP ordering is applied, as shown in Examples.
Properties
Usage notes
- Use ARRAY_SORT(array_agg(value)) for sorted non-NULL values.
- COLLECT_SET keeps at most one NULL; ARRAY_UNIQUE_AGG removes NULL.
- INTEGER and INT mean the same type; STRING means VARCHAR text. NULL can be used with a supported type. Cast NULL explicitly when its type would otherwise be unclear.
- In grouped aggregation, DISTINCT removes duplicate values and retains one NULL. Without DISTINCT, NULL values are skipped.
Examples
Basic example
Ordinary and DISTINCT NULL policies
- ARRAY_SORT displays ascending values with NULL last.
Sliding collection skips NULL
Sort the collected values
A full-group window applies WITHIN GROUP ordering and keeps each input row:
Limitations
- In grouped aggregation, WITHIN GROUP ordering is currently ignored, with or without DISTINCT. ARRAY_SORT gives an ascending grouped result with NULL last.
- WITHIN GROUP accepts only one ordering expression, and it must be the collected value. A separate sort column is rejected. Full-group window calls apply this ordering.
- Sliding ROWS frames are supported for the window syntax shown above.
- RANGE frames with numeric or time offsets are not supported.
- DISTINCT in a window call currently has no effect: duplicate rows still contribute to the result. Use grouped DISTINCT aggregation when duplicate removal is required.