Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 99F012E0110 for ; Wed, 30 Jan 2008 14:00:35 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 77436-03-2 for ; Wed, 30 Jan 2008 14:00:30 -0400 (AST) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id DCE372E00FA for ; Wed, 30 Jan 2008 14:00:31 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id m0UI0UrW014688; Wed, 30 Jan 2008 13:00:30 -0500 (EST) To: Gregory Stark cc: "A. Kretschmer" , pgsql-sql@postgresql.org Subject: Re: Sql ORDER BY and ASC/DESC question In-reply-to: <87tzkvbfr6.fsf@oxford.xeocode.com> References: <20080130104226.GF26490@a-kretschmer.de> <87tzkvbfr6.fsf@oxford.xeocode.com> Comments: In-reply-to Gregory Stark message dated "Wed, 30 Jan 2008 13:32:45 +0000" Date: Wed, 30 Jan 2008 13:00:29 -0500 Message-ID: <14687.1201716029@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200801/230 X-Sequence-Number: 30083 Gregory Stark writes: > If you're not concerned with the planner being able to find indexes to satisfy > these orderings (ie, you don't mind always doing a sort) you could do > something like: > ORDER BY > CASE ? > WHEN 1 THEN name ASC > WHEN 2 THEN name DESC > WHEN 3 THEN height ASC > WHEN 4 THEN height DESC > ELSE id ASC > END Uh, no, putting the ASC/DESC decoration inside a CASE like that is not gonna work --- it's only allowed at the top level of an ORDER BY clause. For numerical sort keys you can cheat by using "-x" in place of "x DESC", but I'm not aware of any equivalent hack for text keys. regards, tom lane