Nicolas Beuzeboc <nicolasb@norchemlab.com> writes:
> I which distinct on was more flexible, it's not happy when the order by
> set is different than the distinct on set.
> I would like to be able to write select distinct on (b,n) b,n,stamp from
> table where ... order by stamp;
Well, no, because it's defined to use the ORDER BY order to determine
which row is "first" within each DISTINCT ON group.
There is an easy workaround for this, which is to sort the rows again in
an outer select:
select * from
( select distinct on (b,n) b,n,stamp from table where ... order by b,n,stamp ) ss
order by stamp;
regards, tom lane