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?