Re: Ordering by a complex field

From: Michael Glaesemann <grzm(at)seespotcode(dot)net>
To: Robert James <srobertjames(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Ordering by a complex field
Date: 2007-07-19 01:30:22
Message-ID: A9E7494A-017F-4F0F-B02A-278768E3A1BA@seespotcode.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Jul 18, 2007, at 20:12 , Robert James wrote:

> I'd like to order so that records where field='2' come first, then
> '1', then
> '9', then anything but '0', then '0'. Is there anyway to do this in a
> standard order by clause (that is, without writing a new SQL
> function)?

# create table whatever (a text primary key);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index
"whatever_pkey" for table "whatever"
CREATE TABLE
# insert into whatever (a) select a::text from generate_series(0,20)
as g(a);
INSERT 0 21
# SELECT a
FROM whatever
ORDER BY a = '2' DESC
, a = '1' DESC
, a = '9' DESC
, a <> '0' DESC;
a
----
2
1
9
5
6
7
8
10
11
12
13
14
15
16
17
18
19
20
3
4
0
(21 rows)

Michael Glaesemann
grzm seespotcode net

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Bruno Lavoie 2007-07-19 03:24:05 Database design : international postal address
Previous Message caij 2007-07-19 01:25:24 postgresql compile problem