这是一个众所周知的问题。考虑以下
EmployeeID EmployeeName Department Salary
----------- --------------- --------------- ---------
1 T Cook Finance 40000.00
2 D Michael Finance 25000.00
3 A Smith Finance 25000.00
4 D Adams Finance 15000.00
5 M Williams IT 80000.00
6 D Jones IT 40000.00
7 J Miller IT 50000.00
8 L Lewis IT 50000.00
9 A Anderson Back-Office 25000.00
10 S Martin Back-Office 15000.00
11 J Garcia Back-Office 15000.00
12 T Clerk Back-Office 10000.00
我们需要找出第二高薪
With Cte As
(
Select
level
,Department
,Max(Salary)
From plc2_employees
Where level = 2
Connect By Prior (Salary) > Salary)
Group By level,Department
)
Select
Employeeid
,EmployeeName
,Department
,Salary
From plc2_employees e1
Inner Join Cte e2 On e1.Department = e2.Department
Order By
e1.Department
, e1.Salary desc
,e1.EmployeeID
某种程度上不起作用......我没有得到正确的结果。有谁可以帮帮我。
答案 0 :(得分:7)
像
这样的东西select * from
(
select EmployeeID, EmployeeName, Department, Salary,
rank () over (partition by Department order by Salary desc) r
from PLC2_Employees
)
where r = 2
编辑 - 测试它,它给出了你期望的答案。
答案 1 :(得分:6)
如果您要自学如何处理CONNECT BY,您应该首先找到适合构造的问题。 CONNECT BY用于处理分层形式的数据,而您的示例则不是。工资以分层方式彼此无关。试图在错误的问题上强制构建一个构造是令人沮丧的,并没有真正教你什么。
查看可以与Oracle一起安装的演示HR模式中的经典员工 - 经理关系。所有员工都向经理报告,包括经理(除了顶级人员)。然后,您可以使用此架构创建查询,以显示公司的组织结构图。
答案 2 :(得分:4)
START WITH … CONNECT BY
旨在通过探索所有可能的降序路径来探索形成图表的数据。您可以在START WITH
子句中指定根节点,在CONNECT BY
子句中指定节点连接(不在WHERE
子句中)。
WHERE
子句过滤器将在层次条件后处理,GROUP BY
和HAVING
相同(当然因为计算GROUP BY
在WHERE
)之后。
因此,你必须在这里CONNECT BY PRIOR department = department
。当存在中间工资时,您还必须避免在两个工资之间完成节点连接。
因此,最终查询将类似于:
SELECT level
, Department
, Salary
FROM plc2_employees pe1
START WITH pe1.salary = (select max(salary) from plc2_employees pe2 WHERE pe2.Department = pe1.Department)
CONNECT BY PRIOR pe1.Department = pe1.Department
AND PRIOR pe1.Salary > pe1.Salary
AND PRIOR pe1.Salary = ( SELECT MIN(Salary) FROM plc2_employees pe3
WHERE pe3.Department = pe1.Department
AND pe3.Salary > pe1.Salary
)
递归条件表明子行和父行之间没有中间工资。
请注意,这实际上是无效的......
答案 3 :(得分:1)
试试这个,它给出了第二高的薪水
select MAX(Salary) as Salary
from Employee_salary
where Salary not in (select MAX(Salary) from Employee_salary)
答案 4 :(得分:0)
试试这个, 它的薪水排名第二......
select MAX(Salary) as Salary
from Employee_salary
where Salary not in (select MAX(Salary) from Employee_salary )
如果你想找到第n个最高薪水,你可以使用以下查询.... 你需要做一个改变..... 将N = n的值设为最高
干杯....:)
SELECT * FROM Employee_salary Emp1
WHERE (N-1) = (SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee_salary Emp2
WHERE Emp2.Salary > Emp1.Salary)
答案 5 :(得分:0)
从员工表中找到第二高的薪水,列为薪水: 数据库:DB2
with t as
(
select distinct salary from employee order by salary desc
),
tr as
(
select salary, row_Number() over() r from t
)
select salary from tr where r = 2
答案 6 :(得分:0)
这可行 -
从员工中选择最低(薪水)
(按工资描述从员工订单中选择前2名薪水)
答案 7 :(得分:0)
您可以使用此查询:
select * from
employee e1
where 2 = (select count (distinct (salary))
from employee e2
where e2.salary >=e1.salary);
答案 8 :(得分:-2)
首先,按降序选择不同的工资(从最大到最小),从该组中选择前2并按升序排列(将数字2放在顶部),然后从这2个中选择前1:
select top 1 s.Salary
from
(select top 2 t.Salary
from
(select distinct Salary
from PLC2_Employees
order by Salary desc) t
order by Salary asc) s