Looks great! I had been slowly thinking along similar lines via the
SELECT min(x) FROM tab WHERE...
SELECT min(x) FROM (SELECT x FROM tab WHERE ... ORDER BY x LIMIT 1) AS t
However, it looks like your approach is more flexible than this :-)
Tom Lane wrote:
> I realized today that this may not be as hard as I thought.
> Specifically, I'm imagining that we could convert
> SELECT min(x), max(y) FROM tab WHERE ...
> into sub-selects in a one-row outer query:
> SELECT (SELECT x FROM tab WHERE ... ORDER BY x LIMIT 1),
> (SELECT y FROM tab WHERE ... ORDER BY y DESC LIMIT 1);
> Breaking it down like that means we can consider each aggregate
> independently, which definitely simplifies matters.