SQL Flow Field Guide

geometry::CollectionAggregate

Catalog support for SQL Server 2022 (16.x) and 2025 (17.x)

spatial aggregate CollectionAggregate

What changes

Input
spatial values of each group
Output or change
one geometry/geography per group
Return type
SQL Server return type: geometry

Syntax at a glance

CollectionAggregate ( geometry_operand )

Work example

Use geometry.CollectionAggregate to collect grouped shapes into one collection result.

Group compatible SRIDs and retain source keys when each input must remain traceable. Keep the geometry type and SRID in the review.

Check your work CollectionAggregate returns one GeometryCollection containing the input point and line.

Practice SQL

Open in SQL Flow

Open the SQL for a walkthrough. Run the example in SQL Server to check its result.

-- Planar geometry. SRID 0 coordinates here are synthetic units, not metres.
WITH shapes (shape) AS
(
    SELECT geometry::STGeomFromText(N'POINT(0 0)', 0) AS shape
    UNION ALL
    SELECT geometry::STGeomFromText(N'LINESTRING(0 0, 3 4)', 0) AS shape
)
SELECT geometry::CollectionAggregate(shape).STAsText() AS collected_shapes,
       geometry::CollectionAggregate(shape).STSrid AS result_srid,
       geometry::CollectionAggregate(shape).STIsValid() AS result_is_valid
FROM shapes;

Open Microsoft Learn for full syntax, permissions, and details for your version.