Skip site navigation (1) Skip section navigation (2)

Re: Sql ORDER BY and ASC/DESC question

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Gregory Stark <stark(at)enterprisedb(dot)com>
Cc: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Sql ORDER BY and ASC/DESC question
Date: 2008-01-30 18:00:29
Message-ID: 14687.1201716029@sss.pgh.pa.us (view raw or flat)
Thread:
Lists: pgsql-sql
Gregory Stark <stark(at)enterprisedb(dot)com> 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

In response to

Responses

pgsql-sql by date

Next:From: Gregory StarkDate: 2008-01-30 20:24:09
Subject: Re: Sql ORDER BY and ASC/DESC question
Previous:From: Gregory StarkDate: 2008-01-30 13:32:45
Subject: Re: Sql ORDER BY and ASC/DESC question

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group