假设我有一个 id、user_name、salary 的表员工。如何选择Oracle中薪水第二高的记录?
我google了一下,找到了这个解决方案,是这样的吗?:
select sal from
(select rownum n,a.* from
( select distinct sal from emp order by sal desc) a)
where n = 2;
已经建议了 RANK 和 DENSE_RANK - 根据您的要求,您还可以考虑 ROW_NUMBER():
select * from (
select e.*, row_number() over (order by sal desc) rn from emp e
)
where rn = 2;
RANK()、DENSE_RANK() 和 ROW_NUMBER() 之间的区别归结为:
因此,如果您只想要一名员工(即使有几名员工的薪水第二高),我建议您使用 ROW_NUMBER()。
如果您使用的是 Oracle 8+,则可以像这样使用RANK()
orDENSE_RANK()
函数
SELECT *
FROM (
SELECT some_column,
rank() over (order by your_sort_column desc) as row_rank
) t
WHERE row_rank = 2;
此查询在SQL*PLUS中工作,以找出第二高的薪水-
SELECT * FROM EMP
WHERE SAL = (SELECT MAX(SAL) FROM EMP
WHERE SAL < (SELECT MAX(SAL) FROM EMP));
这是双子查询。
我希望这可以帮助你..
WITH records
AS
(
SELECT id, user_name, salary,
DENSE_RANK() OVER (PARTITION BY id ORDER BY salary DESC) rn
FROM tableName
)
SELECT id, user_name, salary
FROM records
WHERE rn = 2
你应该使用这样的东西:
SELECT *
FROM (select salary2.*, rownum rnum from
(select * from salary ORDER BY salary_amount DESC) salary2
where rownum <= 2 )
WHERE rnum >= 2;
select * from emp where sal=(select max(sal) from emp where sal<(select max(sal) from emp))
所以在我们的emp表中(默认由oracle提供)这里是输出
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7698 BLAKE MANAGER 7839 01-MAY-81 3000 30
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
或者只是您希望显示第二个最高工资
select max(sal) from emp where sal<(select max(sal) from emp)
3000
select * FROM (
select EmployeeID, Salary
, dense_rank() over (order by Salary DESC) ranking
from Employee
)
WHERE ranking = 2;
dense_rank()用于薪水必须相同。因此它给出正确的输出而不是使用rank()。
选择 Max(Salary) 作为 SecondHighestSalary from Employee where Salary not in (select max(Salary) from Employee)
我建议以下两种方法在 Oracle 中实现这一点。
select distinct SALARY
from EMPLOYEE e1
where 1=(select count(DISTINCT e2.SALARY) from EMPLOYEE e2 where
e2.SALARY>e1.SALARY);
这是获取所需输出的非常简单的查询。但是,此查询非常慢,因为内部查询中的每个薪水都与所有不同的薪水进行比较。
select distinct SALARY
from
(
select e1.*, DENSE_RANK () OVER (order by SALARY desc) as RN
from EMPLOYEE e
) E
where E.RN=2;
这是非常有效的查询。它与 DENSE_RANK() 很好地配合使用,它分配连续的排名,而 RANK() 则根据行号分配下一个排名,就像奥运会奖牌一样。
RANK() 和 DENSE_RANK() 之间的区别: https ://oracle-base.com/articles/misc/rank-dense-rank-first-last-analytic-functions
我相信这将实现相同的结果,而无需子查询或排名函数:
SELECT *
FROM emp
ORDER BY sal DESC
LIMIT 1
OFFSET 2
这个查询每次都能帮助我解决这样的问题。将 N 替换为位置..
select *
from(
select *
from (select * from TABLE_NAME order by SALARY_COLUMN desc)
where rownum <=N
)
where SALARY_COLUMN <= all(
select SALARY_COLUMN
from (select * from TABLE_NAME order by SALARY_COLUMN desc)
where rownum <=N
);
select * from emp where sal = (
select sal from
(select rownum n,a.sal from
( select distinct sal from emp order by sal desc) a)
where n = 2);
这是更优化的,它适合所有场景......
select max(Salary) from EmployeeTest where Salary < ( select max(Salary) from EmployeeTest ) ;
这将适用于所有数据库。
您可以使用两个 max 函数。假设从 SALARY_TBL 中获取 userid=10 及其第二高薪水的数据。
select max(salary) from SALARY_TBL
where
userid=10
salary <> (select max(salary) from SALARY_TBL where userid=10)
用你的最大数字替换 N
SELECT *
FROM Employee Emp1
WHERE (N-1) = (
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)
解释
如果您以前没有见过类似的查询,上面的查询可能会非常混乱——内部查询是所谓的相关子查询,因为内部查询(子查询)使用外部查询(在本例中为 Emp1 表)中的值) 在它的 WHERE 子句中。
和来源
顺便说一句,我将此问题标记为重复。
为 Sql 服务器语法化它
SELECT MAX(Salary) as 'Salary' from EmployeeDetails
where Salary NOT IN
(
SELECT TOP n-1 (SALARY) from EmployeeDetails ORDER BY Salary Desc
)
要获得员工的第二高薪水,我们需要将“n”替换为 2,我们的查询如下
SELECT MAX(Salary) as 'Salary' from EmployeeDetails
where Salary NOT IN
(
SELECT TOP 1 (SALARY) from EmployeeDetails ORDER BY Salary Desc
)
员工工资第三高
SELECT MAX(Salary) as 'Salary' from EmployeeDetails
where Salary NOT IN
(
SELECT TOP 2 (SALARY) from EmployeeDetails ORDER BY Salary Desc
)
SELECT * FROM EMP WHERE SAL=(SELECT MAX(SAL) FROM EMP WHERE SAL<(SELECT MAX(SAL) FROM EMP));
(或者)
SELECT ENAME ,SAL FROM EMP ORDER BY SAL DESC;
(或者)
SELECT * FROM(SELECT ENAME,SAL ,DENSE_RANK()
OVER(PARTITION BY DEPTNO ORDER BY SAL DESC) R FROM EMP) WHERE R=2;
select salary from EmployeeDetails order by salary desc limit 1 offset (n-1).
如果您想找到第二高而不是替换n
为2
.