Re: ORDER BY

From: Alban Hertroys <alban(at)magproductions(dot)nl>
To: George Pavlov <gpavlov(at)mynewplace(dot)com>
Cc: Alexander Staubo <alex(at)purefiction(dot)net>, MicroUser <a(dot)shafar(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: ORDER BY
Date: 2006-11-16 08:58:47
Message-ID: 455C2847.5080709@magproductions.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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(at)magproductions(dot)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 //

In response to

Browse pgsql-general by date

  From Date Subject
Next Message bayerj 2006-11-16 09:11:10 Postgres misses readline
Previous Message SunWuKung 2006-11-16 08:48:08 calculated default