Use distinct on:

select distinct on (t.country) r.*
from rates r
order by t.country, kg_to desc;

Or window functions:

select r.*
from (select r.*,
             row_number() over (partition by country order by kg_to desc) as seqnum
      from rates r
     ) r
where seqnum = 1;

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.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top