Skip to content
Advertisement

Postgresql how to select columns where it matches conditions?

I have a table like this:

I want to select rows where most recent date with corresponding columns, This is what I want to achieve:

I tried to use aggregate but I need inventory_id and customer_id appear at the same time. Is there any method that could do this?

Advertisement

Answer

Use distinct on:

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.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement