Re: LIMIT within UNION?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andrew Perrin <clists(at)perrin(dot)socsci(dot)unc(dot)edu>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: LIMIT within UNION?
Date: 2002-09-12 19:34:07
Message-ID: 9272.1031859247@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Andrew Perrin <clists(at)perrin(dot)socsci(dot)unc(dot)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

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Roland Roberts 2002-09-12 19:35:14 Re: LIMIT within UNION?
Previous Message Jeff Hoffmann 2002-09-12 19:19:01 Re: [SQL] Latitude / Longitude