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
-- Comma-separated concatenation over one group
-- Inner ORDER BY picks concatenation order before joining
RETURN
    f1 := ZIP(ARRAY(1, 1) AS g, ARRAY('a', 'b') AS s).TRANSFORM(
        DEFINE
            p := STRING_AGG(s, ', ' ORDER BY s) GROUP BY (g)
        RETURN p
    ).UNWRAP_ONE(),
    f2 := ZIP(ARRAY(1, 1) AS g, ARRAY(2, 1) AS ord, ARRAY('b', 'a') AS s).TRANSFORM(
        DEFINE
            p := STRING_AGG(s, '|' ORDER BY ord) GROUP BY (g)
        RETURN p
    ).UNWRAP_ONE()
;
Result
f1 VARCHARf2 VARCHAR
a, ba|b