LISTAGG
This section contains reference documentation for the LISTAGG function.
Aggregates string values from rows into a single delimited string. An optional delimiter can be specified (defaults to comma). Use the optional DISTINCT keyword to include only distinct values.
Signature
LISTAGG(colName)
LISTAGG(colName, delimiter)
LISTAGG(DISTINCT colName, delimiter)
WITHIN GROUP clause
Use the WITHIN GROUP (ORDER BY ...) clause to control the order of values in the concatenated result. Without this clause the order of values is undefined.
LISTAGG(colName, delimiter) WITHIN GROUP (ORDER BY sortCol)
LISTAGG(DISTINCT colName, delimiter) WITHIN GROUP (ORDER BY sortCol)
Usage Examples
These examples are based on the Batch Quick Start.
Basic aggregation
select LISTAGG(league, '/') AS value
from baseballStats
WHERE playerName = 'Barry Bonds'NL/NL/NL/NL/NL/NL/NL/NL/NL/NL/...
Distinct values
NL, AL
Ordered aggregation with WITHIN GROUP
Concatenate carriers in alphabetical order for each origin–destination pair:
SFO
LAX
AA, DL, UA, …
JFK
BOS
B6, DL, …
Ordered aggregation with GROUP BY
PIT
Barry Bonds | Bobby Bonilla | …
Last updated
Was this helpful?

