STRING_AGG() GROUP BY ...

All functions > GROUP BY > STRING_AGG() GROUP BY ...

Concatenates string values in the group, separated by the given delimiter; optional inner ORDER BY controls concatenation order.

Syntax

STRING_AGG(expr, delimiter [ORDER BY ...])
 [ FILTER (WHERE condition) ]
  [ GROUP BY feature [, feature ...] ]

Notes

  • NULL values are ignored where the underlying aggregate skips NULLs
  • Can be used with FILTER (WHERE condition) and GROUP BY like other aggregates
  • Inner ORDER BY uses the same shape as ARRAY_AGG(). LIMIT/OFFSET inside the parentheses are not supported; use ARRAY_AGG(expr ORDER BY ... LIMIT n).JOIN(delimiter) instead.

Examples

FeatureQL
SELECT
    f1 := ZIP(ARRAY[1, 1] AS g, ARRAY['a', 'b'] AS s).TRANSFORM(SELECT STRING_AGG(s, ', ' ORDER BY s) GROUP BY g).UNWRAP_ONE(), -- Comma-separated concatenation over one group
    f2 := ZIP(ARRAY[1, 1] AS g, ARRAY[2, 1] AS ord, ARRAY['b', 'a'] AS s).TRANSFORM(SELECT STRING_AGG(s, '|' ORDER BY ord) GROUP BY g).UNWRAP_ONE() -- Inner ORDER BY picks concatenation order before joining
;
Result
f1 VARCHARf2 VARCHAR
a, ba|b

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