Re: CASE in ORDER BY clause

From: Gregory Stark <stark(at)enterprisedb(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: CASE in ORDER BY clause
Date: 2007-07-07 09:33:46
Message-ID: 87sl80woat.fsf@oxford.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Louis-David Mitterrand" <vindex+lists-pgsql-general(at)apartia(dot)org> writes:

> However if I try:
>
> select start_date from show_date order by 'start_date desc';
>
> I get a:
>
> ERROR: non-integer constant in ORDER BY

The quotes mean you are asking to sort by the string "start_date desc" which
is an error because sorting by a constant value is pointless.

Moreover, "asc" and "desc" are part of the order by and have to go after the
expression. Look more carefully at the original suggestion:

On Fri, Jul 06, 2007 at 08:02:54PM +0400, Viatcheslav Kalinin wrote:
>
> # select start_date from show_date
> # order by
> # case when start_date > CURRENT_DATE then start_date end desc,
> # case when start_date <= CURRENT_DATE then start_date end asc;

This is two sorting expressions, one of which will be null for all rows but
which one is null will vary from row to row.

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Евгений Кононов 2007-07-07 09:34:04 Hyper-Trading
Previous Message Louis-David Mitterrand 2007-07-07 08:37:12 Re: CASE in ORDER BY clause