Sql 我如何获得最高工资行,按国家分组

Sql 我如何获得最高工资行,按国家分组,sql,aggregate-functions,Sql,Aggregate Functions,我需要获得日本和印度的最高工资,地址和姓名。有一个子查询,返回每个国家的最高工资。加入该结果以获得最高工资的用户 ------------------------------------------------ country | salary | name | adress ------------------------------------------------ India | 10000 | ch1 | something japa

我需要获得日本和印度的最高工资,地址和姓名。

有一个子查询,返回每个国家的最高工资。加入该结果以获得最高工资的用户

------------------------------------------------
country |   salary  |   name    |   adress  
------------------------------------------------
India   |   10000   |   ch1     |   something
japan   |   20000   |   ch2     |   nothing
india   |   25000   |   ch3     |   hjsdj
japan   |   30000   |   ch4     |   hjhjhj

如果是平局,将返回两个用户(即多个用户的最高工资相同)。

如果您的DBMS碰巧支持ROW_NUMBER()OVER()函数,您可以尝试

select t1.*
from tablename t1
join (select country, max(salary) as max_salary
      from tablename group by country) t2
  on t1.country = t2.country and t1.salary = t2.max_salary

注意,与jarlh的查询相反,它将只返回一个国家的任意一行相同的最高工资行。

您使用的是哪种DBMS?这可以使用窗口函数轻松解决。
select * from
  (select ROW_NUMBER() OVER(PARTITION BY country ORDER BY salary DESC) as rn
    , country, salary as max_salary, name, adress
  from tablename
  ) t
where rn = 1