Skip to content
Advertisement

Postgresql – How to identify latest record of a combination of 2 columns (Example inside)

In PostgreSQL, if i have this kind of record :

I want to add extra column (by a Select query) that identifies if a combination of corona_country and corona_patient is having the latest date (of corona_suspected column) among its own records. So it’ll look like this :

How can i achieve this by sql query? Is there any method that joins another table that records latest dates of each combination of corona_country and corona_patient?

Advertisement

Answer

One option would be using MAX() OVER (PARTITION BY..) analytic function

OR alternatively

Use DENSE_RANK() analytic function

in order to return the result with ties(more than one latest values for corona_suspected columns for each groups)

Demo

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