Re: ORDER BY - Mailing list pgsql-general

From Alban Hertroys
Subject Re: ORDER BY
Date
Msg-id 455C2847.5080709@magproductions.nl
Whole thread Raw
In response to Re: ORDER BY  ("George Pavlov" <gpavlov@mynewplace.com>)
List 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@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 //

pgsql-general by date:

Previous
From: "SunWuKung"
Date:
Subject: calculated default
Next
From: "Albe Laurenz"
Date:
Subject: Re: dependency on 32 bit libpq.so on 64 bit Postgres server.