------------------------------------------------
country | salary | name | adress
------------------------------------------------
India | 10000 | ch1 | something
japan | 20000 | ch2 | nothing
india | 25000 | ch3 | hjsdj
japan | 30000 | ch4 | hjhjhj我需要在日本和印度获得最高工资,并注明姓名。
发布于 2016-04-20 07:06:12
有一个子查询,返回每个国家的最高工资。加入该结果,以获得具有最高薪资的用户。
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如果是平数(即几个最高工资相同的用户),将返回两个用户。
发布于 2016-04-20 07:59:49
如果您的DBMS碰巧支持ROW_NUMBER() OVER()函数,您可以尝试
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 注意,与jarlh的查询相反,它将只返回国家的任意一行相同的最高工资行。
https://stackoverflow.com/questions/36736161
复制相似问题