Re: Sorting by an arbitrary criterion

Поиск
Список
Период
Сортировка
Искать
От
hubert depesz lubaczewski
Тема
Re: Sorting by an arbitrary criterion
Дата
Msg-id
20090709181109.GB25676@depesz.com
Ответ на
Список
Дерево обсуждения
Best way to load test a postgresql server "Peter Sheats" <psheats@pbpost.com>
Re: Best way to load test a postgresql server Shaul Dar <shauldar@gmail.com>
Re: Best way to load test a postgresql server Greg Smith <gsmith@gregsmith.com>
Re: Best way to load test a postgresql server Shaul Dar <shauldar@gmail.com>
Re: Best way to load test a postgresql server Tom Lane <tgl@sss.pgh.pa.us>
Re: Best way to load test a postgresql server Erik Aronesty <erik@q32.com>
Sorting by an arbitrary criterion Craig James <craig_james@emolecules.com>
Re: Sorting by an arbitrary criterion hubert depesz lubaczewski <depesz@depesz.com>
Re: Sorting by an arbitrary criterion "Hartman, Matthew" <Matthew.Hartman@krcc.on.ca>
Re: Sorting by an arbitrary criterion Grzegorz Jaśkiewicz <gryzman@gmail.com>
Re: Sorting by an arbitrary criterion Tom Lane <tgl@sss.pgh.pa.us>
Re: Sorting by an arbitrary criterion Grzegorz Jaśkiewicz <gryzman@gmail.com>
Re: Sorting by an arbitrary criterion "Kevin Grittner" <Kevin.Grittner@wicourts.gov>
Re: Sorting by an arbitrary criterion Alexander Staubo <alex@bengler.no>
Re: Best way to load test a postgresql server Alan McKay <alan.mckay@gmail.com>
Re: Best way to load test a postgresql server Dimitri Fontaine <dfontaine@hi-media.com>
Re: Best way to load test a postgresql server "Kenneth Cox" <kenstir@gmail.com>
Re: Best way to load test a postgresql server Dimitri Fontaine <dfontaine@hi-media.com>
Re: Best way to load test a postgresql server Shaul Dar <shauldar@gmail.com>
Re: Best way to load test a postgresql server "Kenneth Cox" <kenstir@gmail.com>
On Thu, Jul 09, 2009 at 09:26:42AM -0700, Craig James 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.
> I could create a temporary table with the category-to-key mapping, but is there any way to do this in a single SQL statement?

You can do it like this:

select c.*
from categories c, ( values (1, 'z'), (2, 'a'), (3, 'b'), (4, 'w'), (5, 'h') ) as o (id, ordering) on c.id = o.id
order by o.ordering

depesz

-- 
Linkedin: http://www.linkedin.com/in/depesz  /  blog: http://www.depesz.com/
jid/gtalk: depesz@depesz.com / aim:depeszhdl / skype:depesz_hdl / gg:6749007
В списке pgsql-performance по дате отправления
От: Hartman, Matthew
Дата:
От: Hartman, Matthew
Дата:
FAQ