单行子查询返回多行

时间:2011-09-11 22:31:18

标签: sql oracle

我需要一些oracle sql的帮助。问题:我有2个员工和部门表。我从一个查询得到了平均部门薪水,我想用它来看看有多少员工赚的钱多于他们部门的平均水平。到目前为止,我有这个。

此查询返回部门的平均值:

select ROUND(AVG(Salary), 2) Dept_avg_sal
 from employee, department
 where department.department_id = employee.department_id
 group by department_name

我想做的是:

select employee_name,
       salary,
       d.department_name
from   employee e,
       department d
where  salary > (select ROUND(AVG(Salary), 2) Dept_avg_sal
                 from   employee,
                        department
                 where  department.department_id = employee.department_id
                 group  by department_name)  

我得到的错误是:01427。 00000 - “单行子查询返回多行”

我知道同一部门的2名员工赚的钱多于平均水平,我认为这是造成这个问题的原因。

EMPLOYEE_NAME       - SALARY -                -DEPARTMENT_NAME-      DEPT_AVG_SAL           
-------------------- ---------------------- -------------------- ------------ 
FISHER              - 3000.00  -              SALES      -          2500.00                   
JONES          -      3000.00               - ACCOUNTING         -  2750.00                   
KING             -    5000.00           -     EXECUTIVE     -       4500.00                   
**SCOTT           -     2500.00           -     IT             -      2100.00                   
SMITH           -     2900.00          -      IT     -              2100.00**                   
WILSON           -    3000.00          -      RESEARCH      -       2633.33 

任何帮助都会非常感激。

3 个答案:

答案 0 :(得分:7)

您的初始查询缺少外部查询的任何连接条件以及内部查询中的任何相关条件,这些条件将仅限于感兴趣的部门的行。通常,您也不希望group by name,因为大概是id SELECT e.employee_name, e.salary, d.department_name FROM employee e JOIN department d ON d.department_id = e.department_id WHERE e.salary > (SELECT ROUND(AVG(Salary), 2) Dept_avg_sal FROM employee e2 WHERE e2.department_id = e.department_id)

解决这些问题以修复相关子查询得出

SELECT e.employee_name,
       e.salary,
       d.department_name
FROM   employee e
       JOIN department d
         ON d.department_id = e.department_id
       JOIN (SELECT ROUND(AVG(Salary), 2) Dept_avg_sal,
                    department_id
             FROM   employee
             GROUP  BY department_id) e2
         ON e2.department_id = e.department_id
            AND e.salary > e2.Dept_avg_sal  

但是你可能会发现放弃标量相关的子查询并用派生表替换效果会更好。

SELECT employee_name,
       salary,
       d.department_name
FROM   (SELECT employee_name,
               salary,
               d.department_name,
               AVG(Salary) OVER (PARTITION BY e.department_id) AS AvgSalary
        FROM   employee e
               JOIN department d
                 ON d.department_id = e.department_id)
WHERE  salary > AvgSalary  

对于Oracle,我认为以下也应该有效

{{1}}

答案 1 :(得分:1)

> operator只接受一个值,因此你的内部SELECT必须返回1行。我的猜测是你获得了多行。查看内部SELECT返回的内容并尝试LIMIT 1。

答案 2 :(得分:1)

我认为你应该在子查询中添加额外的d.department_id = department.department_id条件(未经测试):

select employee_name,
       salary,
       d.department_name
from   employee e,
       department d
where  salary > (select ROUND(AVG(Salary), 2) Dept_avg_sal
                 from   employee,
                        department
                 where  department.department_id = employee.department_id 
                 AND d.department_id = department.department_id
                 group  by department_name)  

或者只写:

select e.employee_name,
       e.salary,
       d.department_name
from   employee e,
       department d
where  e.department_id = d.department_id
       AND salary > (select ROUND(AVG(Salary), 2) Dept_avg_sal
                 from employee
                 where e.department_id = employee.department_id)