SQL: Union of polygons

后端 未结 4 1274
一向
一向 2021-01-06 07:36

I have a table with a single column of geometry type, containing polygons. How do I get the union of all the polygons in the table?

4条回答
  •  隐瞒了意图╮
    2021-01-06 08:10

    This worked for me:

    CREATE TABLE #g (i INT IDENTITY, a geometry)
    INSERT INTO #g (a)
    VALUES
        (geometry::STGeomFromText(
            'POLYGON((0 0, 3 0, 3 3, 0 3, 0 0))', 0)
        ),
        (geometry::STGeomFromText(
            'POLYGON((5 2, 7 2, 7 0, 5 0, 5 2))', 0)
        )
    
    DECLARE @g geometry
    SELECT TOP 1 @g = a FROM [#g]
    SELECT @g = @g.STUnion(a) FROM #g
    
    SELECT @g
    

    So, apparently, the STUnion method returns null when either the instance on which it's being called or the operand is null, hence the select top 1 hack.

提交回复
热议问题