From: Tom Lane <tgl@sss.pgh.pa.us>
To: Ian McWilton <ian@blazingcactus.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: ORDER BY using specifc values
Date: Thu, 13 Apr 2000 03:03:20 -0400
Message-ID: <9548.955609400@sss.pgh.pa.us> (raw)
In-Reply-To: <200004122331.QAA05037@hedley.blazingcactus.com>
References: <200004122331.QAA05037@hedley.blazingcactus.com>
Ian McWilton <ian@blazingcactus.com> writes:
> I need to return the results of a SELECT ordered by a
> column but in a way that is neither ascending or
> descending.
> The ORDER BY column can have one of five values and I
> want to specify which values are returned in the list
> in which order.
> An example table below shows the data in the DB:
> INDEX | VALUE
> 1, A
> 2, B
> 3, B
> 4, C
> 5, B
> ...and what I want is for my queries result to be
> ordered by VALUE with C put first in the list, then A,
> then B.
> Having investigated it appears that ORDER BY does not
> seem to be any help as it restricts the ordering to ASC
> or DESC.
Make a function f(x) that converts the stored values into a suitable
ordering, say C -> 1, A -> 2, B -> 3. Then do
ORDER BY f(column)
If you have no more-elegant ideas at hand, f() could be defined
using a CASE expression. In fact you could just write the CASE
expression right in ORDER BY, but if you need the same ordering
in many different queries then defining a function is probably
the way to go.
regards, tom lane
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: tgl@sss.pgh.pa.us, ian@blazingcactus.com
Subject: Re: ORDER BY using specifc values
In-Reply-To: <9548.955609400@sss.pgh.pa.us>
References: <200004122331.QAA05037@hedley.blazingcactus.com> <9548.955609400@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox