Skip to content
Advertisement

Query to find value in column dependent on a different column in table being the minimum date

I have a dataset that looks like this. I would like to pull a distinct id, the minimum date and value on the minimum date.

This code will pull the id and the minimum date

How can I get the value on the minimum date so the output of my query looks like this?

Advertisement

Answer

Use distinct on:

This can take advantage of an index on (id, date) and is typically the fastest way to do this operation in Postgres.

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