From: John Sidney-Woollett <johnsw@wardbrook.com>
To: George Pavlov <gpavlov@mynewplace.com>
Cc: Alexander Staubo <alex@purefiction.net>
Cc: MicroUser <a.shafar@gmail.com>
Cc: pgsql-general@postgresql.org
Subject: Re: ORDER BY
Date: Thu, 16 Nov 2006 06:43:40 +0000
Message-ID: <455C089C.50202@wardbrook.com> (raw)
In-Reply-To: <8C5B026B51B6854CBE88121DBF097A865D43BB@ehost010-33.exch010.intermedia.net>
References: <8C5B026B51B6854CBE88121DBF097A865D43BB@ehost010-33.exch010.intermedia.net>
Alternative options for what they're worth - you'd have to explain to
see how efficient they are
select id, name from (
select lower(name) as sortkey, id, name from table where name != 'Other'
union
select 'zzzzz' as sortkey, id, name from table where name = 'Other'
) as t
order by sortkey
select id, name from (
select case when name='Other' then 'zzzzz' else lower(name) end as
sortkey, id, name from table
) as t
order by sortkey
Notice that the sort will be case insensitive in these examples which
may be something that you also want.
John
George Pavlov wrote:
>> For larger tables, you may have to resort to a
>> union:
>>
>> select * from foo where name != 'Other' order by name
>> union
>> select * from foo where name = 'Other'
>
> Alas, this suggestion is wrong on two counts: (a) UNION expects a single
> ORDER BY that applies to the whole recordset and which has to come at
> the end; (b) UNION re-sorts anyway (it needs to eliminate the dupes) --
> maybe you are thinking UNION ALL? So, to follow your advice he may want
> a query like this, although it seems quite silly and there still isn't
> an ironclad guarantee re. the final result sorting:
>
> select * from
> (select * from foo where name != 'Other' order by name) x
> union all
> select * from foo where name = 'Other'
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: In versions below 8.0, the planner will ignore your desire to
> choose an index scan if your joining column's datatypes do not
> match
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-general@postgresql.org
Cc: johnsw@wardbrook.com, gpavlov@mynewplace.com, alex@purefiction.net, a.shafar@gmail.com
Subject: Re: ORDER BY
In-Reply-To: <455C089C.50202@wardbrook.com>
References: <8C5B026B51B6854CBE88121DBF097A865D43BB@ehost010-33.exch010.intermedia.net> <455C089C.50202@wardbrook.com>
* 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