st_collect_agg function
Applies to: Databricks SQL
Databricks Runtime 19 and above
Lakehouse//RT
Support for GEOGRAPHY values is in Public Preview. Support for GEOMETRY values is generally available.
This feature is not available on Databricks SQL Classic warehouses. To learn more about Databricks SQL warehouses, see SQL warehouse types.
Collects all the GEOGRAPHY or GEOMETRY values in a column into a single multipoint, multilinestring, multipolygon, or geometry collection, or returns NULL if the column has zero rows or contains only NULL values.
Syntax
st_collect_agg ( geoCol )
Arguments
geoCol: A column of typeGEOGRAPHYorGEOMETRY.
Returns
A value of the same type as geoCol, representing a multipoint, multilinestring, multipolygon, or geometry collection.
NULL values in the column are ignored. The type of the output depends on the types of the non-NULL input values:
- If all non-
NULLvalues are points, returns a multipoint. - If all non-
NULLvalues are linestrings, returns a multilinestring. - If all non-
NULLvalues are polygons, returns a multipolygon. - Otherwise, returns a geometry collection.
Each output contains one element per non-NULL input value, including empty input values.
Multi-typed inputs (multipoint, multilinestring, multipolygon) and geometry collection inputs are preserved as elements of the resulting geometry collection. They are not flattened.
The order of the elements in the output is not deterministic.
The SRID value of the output is the common SRID value of the non-NULL input values.
The coordinate dimension of the output is the maximum common coordinate dimension of the non-NULL input values.
The function returns NULL if the column has zero rows or contains only NULL values. In contrast, the st_collect function returns an empty geometry collection for an empty or all-NULL array.
Error conditions
- If any two non-
NULLinput values have different SRID values, the function returns ST_DIFFERENT_SRID_VALUES.
Examples
-- Collects geometries of different kinds into a geometry collection.
> SELECT st_astext(st_collect_agg(st_geomfromtext(*))) FROM (SELECT * FROM VALUES('POINT(1 2)'),('LINESTRING(0 0,1 1)'));
GEOMETRYCOLLECTION(POINT(1 2),LINESTRING(0 0,1 1))
-- Collects the geometries of each group.
> SELECT grp, st_astext(st_collect_agg(g)) FROM (SELECT st_geomfromtext('POINT(1 2)') AS g, 'a' AS grp UNION ALL SELECT st_geomfromtext('LINESTRING(0 0,1 1)'), 'b') GROUP BY grp ORDER BY grp;
a MULTIPOINT((1 2))
b MULTILINESTRING((0 0,1 1))
-- Collects a geography point into a multipoint.
> SELECT st_asewkt(st_collect_agg(g)) FROM (SELECT st_geogfromtext('POINT(1 2)') AS g);
SRID=4326;MULTIPOINT((1 2))
-- Returns NULL for an empty result set.
> SELECT st_astext(st_collect_agg(g)) FROM (SELECT st_geomfromtext('POINT(1 2)') AS g WHERE false);
NULL