X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 01BBFD1B4FD for ; Mon, 29 Sep 2003 17:08:25 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 23265-07 for ; Mon, 29 Sep 2003 14:07:57 -0300 (ADT) Received: from wolff.to (wolff.to [66.93.249.74]) by svr1.postgresql.org (Postfix) with SMTP id 4C32CD1B52A for ; Mon, 29 Sep 2003 14:07:55 -0300 (ADT) Received: (qmail 10977 invoked by uid 500); 29 Sep 2003 17:15:03 -0000 Date: Mon, 29 Sep 2003 12:15:03 -0500 From: Bruno Wolff III To: Timo Cc: pgsql-sql@postgresql.org Subject: Re: Need to overcome UNION / ORDER BY restriction Message-ID: <20030929171503.GA10856@wolff.to> Mail-Followup-To: Timo , pgsql-sql@postgresql.org References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.4i X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/393 X-Sequence-Number: 15371 On Mon, Sep 29, 2003 at 17:27:47 +0300, Timo wrote: > > I'd need to get a result set where rows are sorted according to these rules: > > 1. first all rows with priority = 1 sorted according to seniority > 2. then the rest of the rows sorted by seniority, priority You can do something like: bruno=> select * from foo order by priority <> 1, seniority, priority; id | priority | seniority -----+----------+----------- 902 | 1 | 271 935 | 1 | 276 924 | 1 | 581 902 | 2 | 271 902 | 3 | 271 972 | 2 | 275 935 | 2 | 276 924 | 2 | 581 924 | 3 | 581 (9 rows)