SQL Flow Field Guide

geometry::UnionAggregate

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

spatial aggregate UnionAggregate

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

UnionAggregate ( geometry_operand )

Work example

Use geometry.UnionAggregate to combine a grouped set of shapes into a union.

Group compatible SRIDs and test separated, touching, and overlapping inputs. Keep the geometry type and SRID in the review.

Check your work The union has area 24 square coordinate units. Aggregate inputs share SRID 0.

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'POLYGON((0 0, 4 0, 4 4, 0 4, 0 0))', 0) AS shape
    UNION ALL
    SELECT geometry::STGeomFromText(N'POLYGON((2 0, 6 0, 6 4, 2 4, 2 0))', 0) AS shape
)
SELECT geometry::UnionAggregate(shape).STAsText() AS unioned_region,
       geometry::UnionAggregate(shape).STSrid AS result_srid,
       geometry::UnionAggregate(shape).STIsValid() AS result_is_valid
FROM shapes;

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