Skip to content
Advertisement

Select only newer records among the result of a query that extracts duplicate rows in a db

I have a db where I can have multiple rows with same field “aid” (it’s a sort of historical changelog of same items). With the following query, I’m extracting rows with same aid orderd by descending date:

That should be a child table related to a father layer (made by a postgres materialized view that shows only newest aid records) and the following is the result I get:

I’d like to know the SQL syntax to modify the above query in order to show all duplicate rows except the ones with latest dates, so that I can have a table like the following:

Thanks in advance.

Advertisement

Answer

You could do this with window functions. The idea is to rank records having the same aid by descending data_ril, and then filter out the top record per group.

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