Overview
The group_concat function concatenates a group of string values into a single string. This function is useful when processing multiple rows or records, allowing you to merge results from the same group into one string, commonly used for report generation and data aggregation scenarios.
Syntax
Parameters
DISTINCT: (Optional) Removes duplicate values before concatenation.expression: The column or expression to concatenate.ORDER BY sort_expr: (Optional) Specifies the sort order of elements in the concatenated result.sep_string: (Optional) The string used as a separator. If omitted,Lakehousedefaults to a comma (,).
Return Results
- Returns a string containing all concatenated non-
NULLvalues, separated by the specified separator.
Examples
- Basic usage:
- Conditionally concatenate strings using the FILTER clause (use
wm_concatinstead; thegroup_concat(expr SEPARATOR sep) FILTER (WHERE ...)syntax is not supported):
- Use ORDER BY to control concatenation order:
- Use DISTINCT to remove duplicates:
Notes
- If the resulting string exceeds the system's maximum length limit,
group_concatmay throw an error. InLakehouse, this limit can be adjusted by setting the table propertycz.storage.write.max.string.bytessystem variable. - When using the
group_concatfunction, note thatNULLvalues are ignored and not included in the final string. - The
group_concat(expr SEPARATOR sep) FILTER (WHERE ...)syntax is not supported and will produce a syntax error. To use a FILTER clause, use the equivalentwm_concat(sep, expr) FILTER (WHERE ...)syntax instead.
