使用排名功能查询第三高薪
select *
from (
select emp_sal,row_number()over() as RANK
from (
select emp_sal
from emp_demo
order by emp_sal desc
)
)K
where K.RANK=3
错误
*ERROR: subquery in FROM must have an alias
LINE 1: ...m (select emp_sal,row_number()over() as RANK from (select em...
^
HINT: For example, FROM (SELECT ...) [AS] foo.
********** Error **********
ERROR: subquery in FROM must have an alias
SQL state: 42601
Hint: For example, FROM (SELECT ...) [AS] foo.
Character: 63*
我无法在RazorSQL中显示第三高的薪水(也在Postgresql中)
答案 0 :(得分:1)
您似乎错过了over
子句的内容。此外,如果有几名员工拥有相同的工资,您应该使用dense_rank
而不是row_number
:
SELECT *
FROM (SELECT *, DENSE_RANK() OVER (ORDER BY emp_sal DESC) AS rk
FROM emp_demo) t
WHERE rk = 3
答案 1 :(得分:1)
以下作品
select * from employee emp1 where (2) = (select count(distinct(emp2.salary))
from employee emp2 where emp2.salary > emp1.salary )
当计数为0时,查询就像一个循环一样工作,它是最高的最大值。
当计数为1时,只有一个最大值大于当前值。因此应该是第二高的。
当计数为2时,有两个值大于当前值。所以应该是第三高。
替代
select distinct(salary) as salary from employee order by salary desc offset 2 limit 1