Skip to main content
1 of 3
tumasgiu
  • 361
  • 4
  • 10

St_Union comes in two flavors, aggregate and simple.

In your query you are using the simple one that is creating the union of two geometries.

What you are doing is cross joining your two tables, and make union of every combination of the geometries of your two tables, resulting in n*m multipolygons, where n is the row count of your first table and m the count of the second one. This is called a cartesian product

To accomplish what you want, you need to use the aggregate version, that will behave like the sum function. Something like

SELECT st_union(geom) FROM (SELECT geom FROM table1 UNION SELECT geom FROM table2) as foo
tumasgiu
  • 361
  • 4
  • 10