| От | Alexander Staubo |
|---|---|
| Тема | Re: Sorting by an arbitrary criterion |
| Дата | |
| Msg-id | 88daf38c0907090939l68cd97dak38360473e6ffd9e8@mail.gmail.com обсуждение |
| Ответ на | Sorting by an arbitrary criterion (Craig James <craig_james@emolecules.com>) |
| Список | pgsql-performance |
On Thu, Jul 9, 2009 at 6:26 PM, Craig James<craig_james@emolecules.com> wrote:
> Suppose I have a large table with a small-cardinality CATEGORY column (say,
> categories 1..5). I need to sort by an arbitrary (i.e. user-specified)
> mapping of CATEGORY, something like this:
>
> 1 => 'z'
> 2 => 'a'
> 3 => 'b'
> 4 => 'w'
> 5 => 'h'
>
> So when I get done, the sort order should be 2,3,5,4,1.
If the object is to avoid a separate table, you can do it with a
"case" statement:
select ... from ...
order by case category
when 1 then 'z'
when 2 then 'a'
when 3 then 'b'
when 4 then 'w'
when 5 then 'h'
end
If you this sounds slow, you're right. But it might perform well
enough for your use case.
A.
В списке pgsql-performance по дате отправления:
Сайт использует файлы cookie для корректной работы и повышения удобства. Нажимая кнопку «Принять» или продолжая пользоваться сайтом, вы соглашаетесь на их использование в соответствии с Политикой в отношении обработки cookie ООО «ППГ», в том числе на передачу данных из файлов cookie сторонним статистическим и рекламным службам. Вы можете управлять настройками cookie через параметры вашего браузера