On Fri, 18 May 2018 06:20:29 -0700 (PDT), artkraw wrote:
> I can imagine why point 2. doesn't work (no junction table?), but why
> doesn't work 3.
>
Artur,
the golden rule to debug any complex SQL query is by
breaking it in smaller/simpler parts, then carefully
checking each single part.
both your cases 2) and 3) are strictly similar,
because both depends on collecting all geometries
from "tab_4".
so the most obvious thing to be done is carefully
checking this simpler query:
SELECT ST_Collect(tab_4.GEOMETRY)
FROM tab_4;
as you can easily check the result of this aggregation
is a MULTIPOLYGON, as shown in the attached figure.
unhappily, as this second diagnostic query confirms, a
MULTIPOLYGON like this one is _INVALID_:
SELECT ST_IsValid(ST_Collect(tab_4.GEOMETRY) )
FROM tab_4;
--------------------
0
and an invalid geometry is hardly expected to produce
valid results.
the reason explaining why this MultiPolygon is invalid
are rather obvious; all individual Polygons in a
MultiPolygon are always expected to be separated, and
must never share any edge.
this MultiPolygon badly violates the rule.
under such a condition we need a different aggregate
function in order to produce a valid result, that is
ST_Union(); notice the difference:
- ST_Collect will blindly aggregate all input
geometries returning a MultiPolygon (that
could possibly be invalid).
- ST_Union will do the same but in a more intelligent
(and slower) way; whenever possible it will attempt
to merge together smaller polygons into bigger
polygons; so in this case it will return just a single
"big square".
now we are finally ready to rewrite our complex SQL
query:
CREATE TABLE sdif_tab_4x AS
SELECT tab_4.Name, ST_SymDifference(ST_Union(tab_4.GEOMETRY),
ST_Collect(tab_xp3.GEOMETRY))
FROM tab_4, tab_xp3;
and this time it nicely works as expected ;-)
bye Sandro