Re: LIMIT within UNION? - Mailing list pgsql-sql

From Tom Lane
Subject Re: LIMIT within UNION?
Date
Msg-id 9272.1031859247@sss.pgh.pa.us
Whole thread Raw
In response to LIMIT within UNION?  (Andrew Perrin <clists@perrin.socsci.unc.edu>)
Responses Re: LIMIT within UNION?  (Andrew Perrin <clists@perrin.socsci.unc.edu>)
List pgsql-sql
Andrew Perrin <clists@perrin.socsci.unc.edu> writes:
> SELECT ... FROM participants
> WHERE typenr=1 AND <eligibility>
> UNION
> SELECT ... FROM participants
> WHERE typenr=2 LIMIT 172
> ORDER BY zip;

I think you need

SELECT * FROM
(
SELECT ... FROM participants
WHERE typenr=1 AND <eligibility>
UNION
(SELECT ... FROM participants
WHERE typenr=2 LIMIT 172)
) ss
ORDER BY zip;

Not sure if the inner set of parens is essential, but it might be.
The outer SELECT superstructure is definitely necessary to give a
place to hang the ORDER BY on.
        regards, tom lane


pgsql-sql by date:

Previous
From: Andrew Perrin
Date:
Subject: LIMIT within UNION?
Next
From: Roland Roberts
Date:
Subject: Re: LIMIT within UNION?