Use distinct on
:
select distinct on (customer_id) t.*
from t
order by customer_id, max desc;
distinct on
is a Postgres extension that returns on row per whatever is in the parentheses. This row is based on the order by
— the first one that appears in the sorted set.
CLICK HERE to find out more related problems solutions.