3

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

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

不知何故不起作用...我没有得到正确的结果。谁能帮帮我。

4

9 回答 9

7

就像是

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

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

于 2011-11-02T15:19:38.430 回答
6

如果您要自学如何处理 CONNECT BY,则应首先找到适合该构造的问题。CONNECT BY 用于处理分层形式的数据,而您的示例不是。薪金之间没有等级关系。试图在错误的问题上强制拟合一个结构是令人沮丧的,并不能真正教给你任何东西。

看看您可以使用 Oracle 安装的演示 HR 模式中的经典员工-经理关系。所有员工都向经理报告,包括经理(最高层除外)。然后,您可以使用此架构创建查询以显示例如公司的组织结构图。

于 2011-11-02T16:36:58.940 回答
4

START WITH … CONNECT BY旨在通过探索所有可能的下降路径来探索形成图形的数据。您在子句中指定根节点START WITH,在子句中(不在子句中)指定节点连接CONNECT BYWHERE

WHERE子句过滤器将在分层条件之后处理,对于 and 相同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
                               )

递归条件表明子行和父行之间没有中间工资。

请注意,这确实效率低下……</p>

于 2011-11-02T16:46:19.300 回答
1

试试这个,它给了第二高的薪水

select MAX(Salary) as Salary 
from Employee_salary 
where Salary not in (select MAX(Salary) from Employee_salary) 
于 2013-09-01T04:02:23.393 回答
1

您可以使用此查询:

select * from 
employee e1 
where 2 = (select count (distinct (salary)) 
from employee e2 
where e2.salary >=e1.salary);
于 2018-05-14T06:03:17.637 回答
0

试试这个,它给了第二高的薪水......

select MAX(Salary) as Salary 
from Employee_salary 
where Salary not in (select MAX(Salary) from Employee_salary )

如果你想找到第 n 高的薪水,你可以使用下面的查询......你只需要做一个改变......把 N = nth high

干杯....:)

SELECT * FROM Employee_salary Emp1 
WHERE (N-1) = (SELECT COUNT(DISTINCT(Emp2.Salary)) 
               FROM Employee_salary Emp2 
               WHERE Emp2.Salary > Emp1.Salary)
于 2013-09-01T05:18:17.463 回答
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
于 2015-02-26T12:26:42.810 回答
-1

这将起作用 -

SELECT MIN(Salary) 
FROM employee 
WHERE salary IN (SELECT TOP 2 salary FROM employee ORDER BY salary DESC)
于 2015-09-02T15:54:10.133 回答
-3

首先,按降序(从大到小)选择不同的薪水,从该集合中选择前 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
于 2011-11-02T15:26:51.370 回答