gpt4 book ai didi

sql - 找到第二高的工资

转载 作者:行者123 更新时间:2023-12-01 07:08:02 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How to find the employee with the second highest salary?

(5 个回答)


10 个月前关闭。




嗯,这是一个众所周知的问题。考虑以下

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

不知何故不起作用......我没有得到正确的结果。任何人都可以帮助我。

最佳答案

就像是

select * from
(
select EmployeeID, EmployeeName, Department, Salary,
rank () over (partition by Department order by Salary desc) r
from PLC2_Employees
)
where r = 2

编辑 - 对其进行了测试,它给出了您期望的答案。

关于sql - 找到第二高的工资,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7982759/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com