Re: CASE in ORDER BY clause - Mailing list pgsql-general

From Viatcheslav Kalinin
Subject Re: CASE in ORDER BY clause
Date
Msg-id 468E67AE.6090704@ipcb.net
Whole thread Raw
In response to CASE in ORDER BY clause  (Louis-David Mitterrand <vindex+lists-pgsql-general@apartia.org>)
Responses Re: CASE in ORDER BY clause
Re: CASE in ORDER BY clause
List 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_dateasc 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
ascend; 
>     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;


pgsql-general by date:

Previous
From: Andreas 'ads' Scherbaum
Date:
Subject: Re: Problem with autovacuum and pg_autovacuum
Next
From: Alvaro Herrera
Date:
Subject: Re: Problem with autovacuum and pg_autovacuum