Menu
Amazon Redshift
Database Developer Guide (API Version 2012-12-01)

LISTAGG Window Function

For each group in a query, the LISTAGG window function orders the rows for each group according to the ORDER BY expression, then concatenates the values into a single string.

Syntax

Copy
LISTAGG(expression [, 'delimiter' ] ) [ WITHIN GROUP (ORDER BY order_list) ] OVER ( [PARTITION BY partition_expression] )

Arguments

aggregate_expression

Any valid expression (such as a column name) that provides the values to aggregate. NULL values and empty strings are ignored.

delimiter

Optional. The string constant that will separate the concatenated values. The default is NULL.

WITHIN GROUP (ORDER BY order_list)

Optional. Specifies the sort order of the aggregated values. Deterministic only if ORDER BY provides unique ordering. The default is to aggregate all rows and return a single value.

OVER

A clause that specifies the window partitioning. The OVER clause cannot contain a window ordering or window frame specification.

PARTITION BY partition_expression

Optional. Sets the range of records for each group in the OVER clause.

Returns

VARCHAR(MAX). If the result set is larger than the maximum VARCHAR size (64K – 1, or 65535), then LISTAGG returns the following error:

Copy
Invalid operation: Result size exceeds LISTAGG limit

Examples

See LISTAGG Window Function Examples.