'distinct on' and 'order by' conflicts of interest - Mailing list pgsql-general

From stephen@thunkit.com
Subject 'distinct on' and 'order by' conflicts of interest
Date
Msg-id 3068.67.21.238.59.1104511701.squirrel@mail.thunkit.com
Whole thread Raw
Responses Re: 'distinct on' and 'order by' conflicts of interest  (Michael Fuhr <mike@fuhr.org>)
Re: 'distinct on' and 'order by' conflicts of interest  (Stephan Szabo <sszabo@megazone.bigpanda.com>)
List pgsql-general
It has come up several times on the various postgresql lists that in order
to get around the requirement of DISTINCT ON parameters matching the first
ORDER BY parameters, wrap the distinct query in a new 'order by' query:

select * from (select distinct on (a) a,b,c from foo order by a) order by c

however, this will not work when ordering by 'a' will put the wrong record
first, making it choose the wrong record via distinct on.  The 'order by
c' superquery no longer has the correct recordset to sort.

I cannot figure out how to have postgresql first sort the results (on
something other than the 'distinct on' parameters) and then do a recordset
culling by only part of the record.  Does anyone have the syntax for this?

pgsql-general by date:

Previous
From: Jaime Casanova
Date:
Subject: Re: [PATCHES] reqd patch
Next
From: Michael Fuhr
Date:
Subject: Re: 'distinct on' and 'order by' conflicts of interest