I have a table with two columns a and b where a is an ID and b is a timestamp. I need to select all of the a's which are distinct but I only care about the most up to date row per ID.
I.e. I need a way of selecting distinct a's conditional on the b values.
Is there a way to do this using DISTINCT ON in postgres?
Cheers
select distinct on (a) a,b from the_table order by a, b desc?select * from ztable t where not exists (select * from ztable x where x.a = t.a and x.b > t.b);distinct on ()to work properly. Have a look at all the other questions tagged with greatest-n-per-group for Postgres.