0

I want to Display department number, names and salaries of employees who are earning max salary in their departments which are in the same table.

I am using oracle sql. The Table structure I am using is

Emp(Empno,Ename,Job,Salary,Deptno)

I have read about this and I think that this can be done by the use of correlated sub-queries. The query I fired was

select E1.Ename,E1.Ename,E1.Salary
from Emp E1
where E1.Empno=(
              select Empno
              from Emp E2
              where Salary=(
                          select max(Salary)
                          from Emp
                          where Deptno=E1.Deptno
                           )
                );

This gives an error saying "single-row subquery returns more than one row". What am I doing wrong? What should do to correct it?

4

1 に答える 1

2
SELECT  EmpNo, Ename,Job,Salary,Deptno
FROM    
    (
        SELECT  EmpNo, Ename,Job,Salary,Deptno,
                DENSE_RANK() OVER (PARTITION BY DeptNo
                                    ORDER BY Salary DESC ) rn
        FROM    Emp
    ) a
WHERE   a.rn  = 1

または使用してMAX

SELECT  a.*
FROM    Emp a
        INNER JOIN
        (
            SELECT  DeptNo, MAX(Salary) Max_sal
            FROM    Emp
            GROUP   BY DeptNo
        ) b ON a.DeptNo = b.DeptNo AND
                a.Salary = b.Max_SAL
于 2013-01-24T07:17:02.060 に答える