pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alban Hertroys <alban@magproductions.nl>
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 09:58:47 +0100
Message-ID: <455C2847.5080709@magproductions.nl> (raw)
In-Reply-To: <8C5B026B51B6854CBE88121DBF097A865D43BB@ehost010-33.exch010.intermedia.net>
References: <8C5B026B51B6854CBE88121DBF097A865D43BB@ehost010-33.exch010.intermedia.net>

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) --

You can also add an imaginary key to sort on, like

select 1, * from foo where name != 'Other'
union all
select 9, * from foo where name = 'Other'
order by 1;

Or independent of column order:

select 1 AS sort_key, * from foo where name != 'Other'
union all
select 9 AS sort_key, * from foo where name = 'Other'
order by sort_key;

Regards,
-- 
Alban Hertroys
alban@magproductions.nl

magproductions b.v.

T: ++31(0)534346874
F: ++31(0)534346876
M:
I: www.magproductions.nl
A: Postbus 416
   7500 AK Enschede

// Integrate Your World //



view thread (27+ messages)  latest in thread

Message-ID: <455C2847.5080709@magproductions.nl>
Permalink:  ../455C2847.5080709@magproductions.nl/
Also on:    postgresql.org/message-id/455C2847.5080709@magproductions.nl

 · 

reply

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: alban@magproductions.nl, gpavlov@mynewplace.com, alex@purefiction.net, a.shafar@gmail.com
  Subject: Re: ORDER BY
  In-Reply-To: <455C2847.5080709@magproductions.nl>
  References: <8C5B026B51B6854CBE88121DBF097A865D43BB@ehost010-33.exch010.intermedia.net> <455C2847.5080709@magproductions.nl>

* 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