Re: CASE in ORDER BY clause

From: Viatcheslav Kalinin <vka(at)ipcb(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: CASE in ORDER BY clause
Date: 2007-07-06 16:02:54
Message-ID: 468E67AE.6090704@ipcb.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Louis-David Mitterrand wrote:
> Hi,
>
> I am trying the following:
>
> critik=# select start_date from show_date order by case when start_date > CURRENT_DATE then start_date desc else start_date asc end;
> ERROR: syntax error at or near "desc"
> LINE 1: ...se when start_date > CURRENT_DATE then start_date desc else
> ...
>
> If I remove the "desc" and "asc" then the quey is accepted but doesn't
> do what I want.
>
> OTOH if I try:
>
> critik=# select start_date from show_date order by start_date case when start_date > CURRENT_DATE then desc else asc end;
> ERROR: syntax error at or near "case"
> LINE 1: ...ect start_date from show_date order by start_date case when ...
>
> How can i order ASC or DESC depending on a condition?
>
> Thanks,
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/docs/faq
>
Correction:

# 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;

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alvaro Herrera 2007-07-06 16:06:41 Re: Problem with autovacuum and pg_autovacuum
Previous Message Viatcheslav Kalinin 2007-07-06 15:59:06 Re: CASE in ORDER BY clause