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

From Lew
Subject Re: CASE in ORDER BY clause
Date
Msg-id u7OdnU2UquGxjg3bnZ2dnUVZ_jCdnZ2d@comcast.com
Whole thread Raw
In response to Re: CASE in ORDER BY clause  (Perry Smith <pedz@easesoftware.com>)
Responses Re: CASE in ORDER BY clause
List pgsql-general
Perry Smith wrote:
> I see.  So, in effect he has:
>
>     ORDER BY NULL DESC, start_date ASC;
>
> or
>
>   ORDER BY start_date DESC, NULL ASC;

Not exactly.  He has (first sort clause) *and* (second sort clause), not "or".

Both sort clauses operate at all rows.

You are sort of correct in that for any given row, its position in the SELECT
order will be determined by exactly one of

(start_date, NULL) for future dates
or
(NULL, start_date) for past dates
.

It is incorrect to see that as two separate ORDER BY clauses.

So if your RDBMS sorts NULLs after all other values, then from
>> 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;

all rows with start_date > CURRENT_DATE will appear first, in start_date
descending order,
then all rows with start_date <= CURRENT_DATE will appear, in start_date
ascending order.

Is CURRENT_DATE evaluated once for the query or twice for each row?

--
Lew

pgsql-general by date:

Previous
From: Евгений Кононов
Date:
Subject: Hyper-Trading
Next
From: Mike
Date:
Subject: Performance Question - Table Row Size