Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id B31EA476C9F for ; Thu, 12 Sep 2002 16:20:03 -0400 (EDT) Received: from davinci.ethosmedia.com (davinci.ethosmedia.com [209.10.40.250]) by postgresql.org (Postfix) with ESMTP id DE4B6476D0C for ; Thu, 12 Sep 2002 16:20:02 -0400 (EDT) Received: from [66.219.92.2] (HELO chocolate-mousse) by davinci.ethosmedia.com (CommuniGate Pro SMTP 3.5.9) with ESMTP id 1646069; Thu, 12 Sep 2002 13:20:00 -0700 Content-Type: text/plain; charset="iso-8859-1" From: Josh Berkus Reply-To: josh@agliodbs.com Organization: Aglio Database Solutions To: Andrew Perrin Subject: Re: LIMIT within UNION? Date: Thu, 12 Sep 2002 13:20:28 -0700 X-Mailer: KMail [version 1.4] Cc: pgsql-sql@postgresql.org References: In-Reply-To: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-Id: <200209121320.28318.josh@agliodbs.com> X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200209/121 X-Sequence-Number: 9398 Andrew, Another approach, one that does not require you to know before constructing= =20 the query how many eligible subjects there are, is not to use a union at al= l: SELECT * FROM ( SELECT ... , zip FROM participants WHERE (typenr =3D 1 and ) DESC LIMIT 200 ) p1 ORDER BY zip; The inner query gives you all of the records that meet the eligibility=20 criteria, plus all of the records that have typenr =3D 2, in the order of= =20 whether or not they meet the criteria (as a boolean value) and truncates it= =20 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. --=20 -Josh Berkus Aglio Database Solutions San Francisco