Re: Dynamic expressions set in "order by" clause

Поиск
Список
Период
Сортировка
От Vyacheslav Kalinin
Тема Re: Dynamic expressions set in "order by" clause
Дата
Msg-id 9b1af80e0711210859x4d7288a2s3d5fc6063c17deb7@mail.gmail.com
обсуждение исходный текст
Ответ на Re: Dynamic expressions set in "order by" clause  (Erik Jones <erik@myemma.com>)
Список pgsql-general
You could write a set of expressions that yield proper order by field
in one case and say null in another. Say we want order by columns
<col1, col2 desc> if the first function argument is '1' and by <col3
desc, col1, col2> if it is '2', this can be achieved as:

select col1, col2, col3 from mytable
 where ...
 order by case when $1 = '1' then col1 end,
              case when $1 = '1' then col2 else col3 end desc,
              case when $1 = '2' then col1 end,
              case when $1 = '2' then col2 end
;

This would work as following when $1 = '1':
select col1, col2, col3 from mytable
 where ...
 order by col1, col2desc, null, null;

and when $1 = '2':
select col1, col2, col3 from mytable
 where ...
 order by null, col3 desc, col1, col2

В списке pgsql-general по дате отправления:

Предыдущее
От: Martin Gainty
Дата:
Сообщение: Re: Read-only availability of a standby server?
Следующее
От: xeb@mail.ru
Дата:
Сообщение: Re: ERROR: invalid restriction selectivity: 224359728.000000