From: | Josh Berkus <josh(at)agliodbs(dot)com> |
---|---|
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 20:20:28 |
Message-ID: | 200209121320.28318.josh@agliodbs.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
Andrew,
Another approach, one that does not require you to know before constructing
the query how many eligible subjects there are, is not to use a union at all:
SELECT * FROM (
SELECT ... , zip
FROM participants
WHERE (typenr = 1 and <eligibility criteria)
OR (typenr = 2)
ORDER BY (typenr = 1 and <eligibility criteria>) DESC
LIMIT 200 ) p1
ORDER BY zip;
The inner query gives you all of the records that meet the eligibility
criteria, plus all of the records that have typenr = 2, in the order of
whether or not they meet the criteria (as a boolean value) and truncates it
at 200 records.
The outer query then re-sorts this result in zip order.
This seems, to me, much more flexible than using a UNION query.
--
-Josh Berkus
Aglio Database Solutions
San Francisco
From | Date | Subject | |
---|---|---|---|
Next Message | Joe Conway | 2002-09-12 20:54:25 | Re: Latitude / Longitude |
Previous Message | Tom Lane | 2002-09-12 20:18:19 | Re: LIMIT within UNION? |