Re: Sorting by an arbitrary criterion - Mailing list pgsql-performance

From Tom Lane
Subject Re: Sorting by an arbitrary criterion
Date
Msg-id 2181.1247157519@sss.pgh.pa.us
Whole thread Raw
In response to Re: Sorting by an arbitrary criterion  (Grzegorz Jaśkiewicz <gryzman@gmail.com>)
Responses Re: Sorting by an arbitrary criterion
List pgsql-performance
=?UTF-8?Q?Grzegorz_Ja=C5=9Bkiewicz?= <gryzman@gmail.com> writes:
> On Thu, Jul 9, 2009 at 5: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:

> you can create translation table, join it, and sort by its key.

Much easier to
    ORDER BY CASE category WHEN 'z' THEN 1 WHEN 'a' THEN 2 ... END

Actually, consider putting the CASE into a function and doing
    ORDER BY sort_order(category)

            regards, tom lane

pgsql-performance by date:

Previous
From: Robin Houston
Date:
Subject: Huge difference in query performance between 8.3 and 8.4 (possibly)
Next
From: "Kevin Grittner"
Date:
Subject: Re: Sorting by an arbitrary criterion