给定表包含
employee_id,
Month_Year,
Salary,
City
编写查询以从每个城市获得年薪第五高的员工。
我的代码:
select * from (select * from(select * from (select * from dummy order by sal desc) where rownum <= 5) order by sal) where rownum <=1 ;
但这仅使总表中的排名第五。
我应该在哪里使用group by语句?
谢谢。
答案 0 :(得分:0)
您可以在下面尝试-
select *
from tablename as t1
where
t1.salary = (select salary from tablename t2
where
t2.city= t1.city ORDER by salary desc limit 1 offset 5)
答案 1 :(得分:0)
以下查询对我有用。 (在条件为“ N”的情况下为number(4),其中“ N”为第n个最高薪水。在您的情况下,您希望第5个最高薪水,因此N-1为4)
find nth highest salary in sql
select salary,city
FROM tablename n1
WHERE (4) = (
SELECT COUNT(DISTINCT(n2.Salary))
FROM tablename n2
WHERE n2.Salary > n1.Salary and n1.city=n2.city group by n2.city)