Skip to content
Advertisement

Get the highest category while performing other type of aggregates

Story:

I am trying to get the sum of records and the max date by country and games and another column that would rank the top countries based on the sum of records:

It is the country rank column that is giving me trouble. Here’s what I tried:

All it does is rank the each row partition by country which is what I expected.

Objective

Is there a way to achieve what i want in one or two subqueries?

Here’s a desired output

Here for ID #2, country USA is 1st due to its combined sum of records from all games.

To the request of comments below:

Raw data looks like that:

Advertisement

Answer

You can build on your aggregation query. This version produces a ranking that is like row_number(), so ties would get different values:

Here is a db<>fiddle.

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