---
title: st_collect_agg function
description: Learn the syntax and behavior of the st\_collect\_agg function of the SQL language in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/gcp/en/sql/language-manual/functions/st_collect_agg
---

# `st_collect_agg` function

**Applies to:** ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks Runtime 19 and above ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Lakehouse//RT

> [!NOTE]
> **Preview**
>
> Support for GEOGRAPHY values is in [Public Preview](https://docs.databricks.com/gcp/en/release-notes/release-types.md). Support for GEOMETRY values is [generally available](https://docs.databricks.com/gcp/en/release-notes/release-types.md).

> [!NOTE]
>
> This feature is not available on Databricks SQL Classic warehouses. To learn more about Databricks SQL warehouses, see [SQL warehouse types](https://docs.databricks.com/gcp/en/compute/sql-warehouse/warehouse-types.md).

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 type `GEOGRAPHY` or `GEOMETRY`.

## 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-`NULL` values are points, returns a multipoint.
* If all non-`NULL` values are linestrings, returns a multilinestring.
* If all non-`NULL` values 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](https://docs.databricks.com/gcp/en/sql/language-manual/functions/st_collect.md) returns an empty geometry collection for an empty or all-`NULL` array.

## Error conditions

* If any two non-`NULL` input values have different SRID values, the function returns [ST\_DIFFERENT\_SRID\_VALUES](https://docs.databricks.com/gcp/en/error-messages/error-classes.md#st_different_srid_values).

## Examples

```sql
-- 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
```

## Related functions

* [`st_collect` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/st_collect.md)
* [`st_dump` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/st_dump.md)
* [`st_envelope_agg` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/st_envelope_agg.md)
* [`st_union_agg` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/st_union_agg.md)
