我指的是以下查询,以查找员工的第N个最高薪水。
select sal from emp t where &n = (select count(sal) from (select distinct sal
from emp) where t.sal<=sal);
一位绅士说这个查询有效。有人可以解释如何将COUNT(实际上是1到X之间的值,其中X是完全不同的工资)等同于&amp; n产生这个结果吗?
我试图了解数据库如何在内部处理此查询并生成结果?
谢谢。
答案 0 :(得分:8)
首先,查询将返回nth
最低工资值。要返回nth
最高薪资值,您必须将t.sal <= sal
更改为t.sal >= sal
。
接下来,此查询的工作原理是首先查找薪资值的不同列表作为一个派生表,然后确定薪水少于此列表中每个薪水的员工数。 t.sal <= sal
正在获取派生表(大多数数据库需要具有别名)并将每个值与外部emp
表进行比较。应该注意的是,在平局的情况下,这将返回多行。
要手动跟踪输出,我们需要一些输入:
Alice | 200
Bob | 100
Charlie | 200
Danielle | 150
Select Distinct sal
From emp
给我们
200
100
150
现在我们分析外表中的每一行
Alice - There are 3 distinct salary values less than or equal to 200
Bob - 1 rows <= 100
Charlie - 3 rows <= 200
Danielle - 2 row <= 150
因此,对于每个工资值,我们得到以下计数(并按计数重新排序):
Bob 1
Danielle 2
Charlie 3
Alice 3
我认为你忽略的最重要的方面是外部emp
表与内部计数计算相关(这就是它被称为相关子查询的原因)。即,对于外emp
表中的每一行,通过t.sal <= sal
计算该行的工资的新计数。同样,大多数数据库系统都要求最内部的查询具有这样的别名(请注意As Z
别名):
Select sal
From emp As t
Where &n = (
Select Count(Z.sal)
From (
Select Distinct sal
From emp
) As Z
Where t.sal <= Z.sal
)
答案 1 :(得分:1)
要获得第n个最高工资值,只需输入'N'的值。
Select Min(Salary) From (Select Top N * From Table_Name Order by Salary Desc);
答案 2 :(得分:1)
select sal
from (
select sal,
dense_rank() over (order by sal desc) as rnk
) t
where rnk = 5;
将where rnk = 5
替换为您想要的任何“nth”。
答案 3 :(得分:0)
SELECT Max(Salary) as Salary
FROM employee
where Salary Not in
(SELECT TOP N Salary FROM employee ORDER BY Salary DESC)
where N is defined by you.
因此,假设您在表员工中有以下薪水: 这里employeeID和Salary是employee表的列。
101 25,000
154 89,000
987 42,000
450 12,000
954 50,000
如果我们想看到第四高薪
25,000
查询返回第四高薪。
答案 4 :(得分:0)
SELECT TOP 1 salary
FROM (
SELECT DISTINCT TOP n salary
FROM employee
ORDER BY salary DESC) a
ORDER BY salary
where n > 1 (n is always
答案 5 :(得分:0)
在数据库记录数据条目中,如
employ_id NAME salary
101 Henry 24000
102 Smith 24000
105 Roy 17000
106 Robbin 15000
702 Mac 2500
708 Bill 2100
709 Kane 2000
710 Ted 2000
这里有些员工的工资相同,然后如何计算第n个(最高/最低)工资
计算第三高薪
select * from emloyees where salary in (select salary from (select rownum rank , salary from (select distinct salary from employees order by salary **desc**)) where rank =3;
ans = 15000
同样计算第3个最低工资 使用较小的更改而不是desc类型asc
键入相同的Queryselect * from emloyees where salary in (select salary from (select rownum rank , salary from (select distinct salary from employees order by salary **asc**)) where rank =3;
希望这会对你有所帮助
答案 6 :(得分:0)
更改第n个最高工资值只需输入'N'
的值SELECT e1.EmployeeName, e1.EmployeeSalary from Employee e1
where N = (
select COUNT(e2.EmployeeSalary) from Employee e2 where e2.EmployeeSalary >= e1.EmployeeSalary)
答案 7 :(得分:0)
有很多方法可以达到这个目的: -
1)
Select Top(1) sal from emp
where sal not in (select DISTINCT top(n-1) sal from emp order by sal desc)
2)
select salary
from (
select salary,
roe_number() over (order by salary ) as row from emp
) emp1
where row= n;
3)
select salary
from (
select salary,
dense_rank() over (order by salary ) as row from emp
) emp1
where row= n;
4)
Select Min(sal) From
(Select DISTINCT Top n * From emp Order by sal Desc)as emp1;
5)
SELECT * FROM emp Emp1
WHERE (n-1) = (
SELECT COUNT(DISTINCT(Emp2.Sal))
FROM emp Emp2
WHERE Emp2.Sal > Emp1.Sal)
答案 8 :(得分:0)
select salary
from (Select ROW_NUMBER() over(order by salary desc) as row ,salary from Employee)as temp
where row=2;
---2nd highest salary
答案 9 :(得分:-1)
查询:
select
ename
,sal
,dense_rank() over (order by sal desc) ranking
from emp;
输出:
ENAME SAL RANKING
KING 5000 1
FORD 3000 2
SCOTT 3000 2
JONES 2975 3
CLARK 2850 4
BLAKE 2850 4
ALLEN 1600 5
绕过一个过滤器并挑出第N个最高薪水,说第四个最高薪水。
查询:
select *
from
(
select ename
,sal
,dense_rank() over (order by sal desc) ranking
from emp
)
where ranking = 4 -- Replace 4 with any value of N
输出:
ENAME SAL RANKING
BLAKE 2850 4
CLARK 2850 4