Re: CASE in ORDER BY clause

Поиск
Список
Период
Сортировка
Искать
От
Lew
Тема
Re: CASE in ORDER BY clause
Дата
Msg-id
u7OdnU2UquGxjg3bnZ2dnUVZ_jCdnZ2d@comcast.com
Ответ на
Список
Дерево обсуждения
CASE in ORDER BY clause Louis-David Mitterrand <vindex+lists-pgsql-general@apartia.org>
Re: CASE in ORDER BY clause Viatcheslav Kalinin <vka@ipcb.net>
Re: CASE in ORDER BY clause Viatcheslav Kalinin <vka@ipcb.net>
Re: CASE in ORDER BY clause Louis-David Mitterrand <vindex+lists-pgsql-general@apartia.org>
Re: CASE in ORDER BY clause Louis-David Mitterrand <vindex+lists-pgsql-general@apartia.org>
Re: CASE in ORDER BY clause Gregory Stark <stark@enterprisedb.com>
Re: CASE in ORDER BY clause Perry Smith <pedz@easesoftware.com>
Re: CASE in ORDER BY clause Martijn van Oosterhout <kleptog@svana.org>
Re: CASE in ORDER BY clause Tom Lane <tgl@sss.pgh.pa.us>
Re: CASE in ORDER BY clause Perry Smith <pedz@easesoftware.com>
Re: CASE in ORDER BY clause Lew <lew@lewscanon.nospam>
Re: CASE in ORDER BY clause "Uwe C. Schroeder" <uwe@oss4u.com>
Re: CASE in ORDER BY clause Tom Allison <tom@tacocat.net>
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 по дате отправления
От: Stefan Christ
Дата:
Сообщение: Day of week vs. Language
От: Mike
Дата:
FAQ