Skip to content
Advertisement

SQL – return rows in partition based on max value

I have below dataset with comments which row must be returned.

Logic is: return value for max kg_to within partition of each country.

Current working code:

Question:

  1. Shorter code would be better, any ideas on how to improve it?

Advertisement

Answer

Use distinct on:

Or window functions:

Note: I also don’t see how your code could be retrieving duplicates, unless you have duplicate maximum values for a country in your table.

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