Keeping me busy with this issue I found the LEAST
and GREATEST
functions in the PostgreSQL documentation.
The following final query (after the subqueries in my question) works, even if this approach is maybe not the most elegant one.
The clue is ordering the results with the LEAST
value of the area.
...
SELECT
objects.n,
-- objects.geom,
ST_Area(objects.geom) AS area
FROM objects, split
WHERE split.n <= ST_NumGeometries(split.geom)
ORDER BY LEAST(ST_Area(ST_GeometryN(objects.geom,split.n)))
-- LIMIT 1
The result set for this is:
n | area
---+------------------
2 | 3199.62422930388
1 | 4603.98865134125
1 | 4603.98865134125
2 | 3199.62422930388
(4 rows)
When I limit this query by 1 and include the geometry column (both commented out in the query above), I have always selected the smallest piece from the split.
Is there a way to avoid the limiting?