Re: Is it possible to make the order of output the same as the order of input parameters?

Поиск
Список
Период
Сортировка
От Tim Landscheidt
Тема Re: Is it possible to make the order of output the same as the order of input parameters?
Дата
Msg-id m3bpbtvdlg.fsf@passepartout.tim-landscheidt.de
обсуждение исходный текст
Ответ на Is it possible to make the order of output the same as the order of input parameters?  ("m. hvostinski" <makhvost@gmail.com>)
Ответы Re: Is it possible to make the order of output the same as the order of input parameters?  (Andreas Kretschmer <akretschmer@spamfence.net>)
Список pgsql-general
Andreas Kretschmer <akretschmer@spamfence.net> wrote:

>> I have a simple query like:

>> SELECT * FROM customer WHERE id IN (23, 56, 2, 12, 10)

>> The problem is that I need to retrieve the rows in the same order as the set of
>> ids provided in the select statement. Can it be done?

> No. The only way is:

> select * from ... where id in (...) order by case when id=23 then 1,
> case when id=56 then 2 end, case when id=2 then 3 end, ...

Or, quick 'n' dirty:

| SELECT * FROM customer
|   WHERE id IN (23, 56, 2, 12, 10)
|   ORDER BY POSITION(':' || id || ':' IN ':23:56:2:12:10:');

When using CASE, make sure you read the documentation to the
end: I stumbled upon "CASE id WHEN 23 THEN 1 WHEN 56 THEN 2
WHEN [...] END" only just recently by pure chance :-).

Tim

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

Предыдущее
От: Alban Hertroys
Дата:
Сообщение: Re: create index concurrently - duplicate index to reduce time without an index
Следующее
От: Andreas Kretschmer
Дата:
Сообщение: Re: Is it possible to make the order of output the same as the order of input parameters?