pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedORDER BY using specifc values
3+ messages / 3 participants
[nested] [flat]
* ORDER BY using specifc values
@ 2000-04-12 23:31 Ian McWilton <ian@blazingcactus.com>
2000-04-13 07:03 ` Re: ORDER BY using specifc values Tom Lane <tgl@sss.pgh.pa.us>
2000-04-13 07:42 ` Re: ORDER BY using specifc values Oliver Mueschke <o@mueschke.de>
0 siblings, 2 replies; 3+ messages in thread
From: Ian McWilton @ 2000-04-12 23:31 UTC (permalink / raw)
To: pgsql-sql
Hi there,
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.
The onyl solution I can see isn't at all elegant; issue
five SELECT statements in sequence and construct one
list from this five
Does anyone know of another way to do what I want in
one query?
Regrads,
Ian McWilton.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: ORDER BY using specifc values
2000-04-12 23:31 ORDER BY using specifc values Ian McWilton <ian@blazingcactus.com>
@ 2000-04-13 07:03 ` Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 0 replies; 3+ messages in thread
From: Tom Lane @ 2000-04-13 07:03 UTC (permalink / raw)
To: Ian McWilton <ian@blazingcactus.com>; +Cc: pgsql-sql
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
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: ORDER BY using specifc values
2000-04-12 23:31 ORDER BY using specifc values Ian McWilton <ian@blazingcactus.com>
@ 2000-04-13 07:42 ` Oliver Mueschke <o@mueschke.de>
1 sibling, 0 replies; 3+ messages in thread
From: Oliver Mueschke @ 2000-04-13 07:42 UTC (permalink / raw)
To: Ian McWilton <ian@blazingcactus.com>; +Cc: pgsql-sql
On Wed, Apr 12, 2000 at 04:31:18PM -0700, Ian McWilton wrote:
> 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
in 6.5.3:
wb_data=> create table x(i int, t text);
CREATE
wb_data=> insert into x values(1,'A');
INSERT 21514 1
wb_data=> insert into x values(2,'C');
INSERT 21515 1
wb_data=> insert into x values(3,'A');
INSERT 21516 1
wb_data=> insert into x values(4,'B');
INSERT 21517 1
wb_data=> select * from x order by t;
i|t
-+-
1|A
3|A
4|B
2|C
(4 rows)
wb_data=> select
wb_data-> i, t,
wb_data-> case
wb_data-> when t='C' then 1
wb_data-> when t='A' then 2
wb_data-> when t='B' then 3
wb_data-> end as oby
wb_data-> from x
wb_data-> order by oby;
i|t|oby
-+-+---
2|C| 1
1|A| 2
3|A| 2
4|B| 3
(4 rows)
oliver
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2000-04-13 07:42 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2000-04-12 23:31 ORDER BY using specifc values Ian McWilton <ian@blazingcactus.com>
2000-04-13 07:03 ` Tom Lane <tgl@sss.pgh.pa.us>
2000-04-13 07:42 ` Oliver Mueschke <o@mueschke.de>
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