【问题标题】:SQL - Trying to get employees with greater than average department salarySQL - 试图让员工的平均工资高于平均部门工资
【发布时间】:2020-08-09 13:49:50
【问题描述】:

我正在尝试获取一个部门的平均工资,然后用它来获取任何高于该平均工资的员工。

我的查询是:

select e.first_name, e.salary, e.department_id from
employees as e
inner join departments as d
on d.id = e.department_id
where salary > (select avg(e.salary)
  from employees as e
  where e.department_id = d.id
  group by e.department_id);

但它只返回一个结果“Ian”。

 first_name | salary | department_id 
------------+--------+---------------
 Ian        |  80000 |             2
(1 row)

        avg         
--------------------
 35000.000000000000
(1 row)

 id | first_name | last_name | salary | department_id 
----+------------+-----------+--------+---------------
  1 | John       | Smith     |  20000 |             1
  2 | Ava        | Muffinson |  10000 |             5
  3 | Cailin     | Ninson    |  30000 |             2
  4 | Mike       | Peterson  |  20000 |             2
  5 | Ian        | Peterson  |  80000 |             2
  6 | John       | Mills     |  50000 |             3
(6 rows)

 id |    name     
----+-------------
  1 | Reporting
  2 | Engineering
  3 | Marketing
  4 | Biz Dev
  5 | Silly Walks
(5 rows)

我做错了什么? 谢谢!

【问题讨论】:

  • 您的查询很好。对于您的示例数据,Ian 是唯一应该返回的结果。
  • 请注意,您不需要在子查询中使用 group by,因为您希望汇总所有结果。

标签: sql postgresql


【解决方案1】:

从一个预查询中的每个部门的薪水开始。这应该是之后所有员工的基础。

select
      e.*,
      JustAvgs.DptAvg
   from
      ( select 
              e.department_id,
              avg(e.salary) DptAvg
           from 
              employees as e
           group by 
              e.department_id) JustAvgs
         JOIN employees e
            on JustAvgs.Department_id = e.Department_ID
           AND e.Salary > JustAvgs.DptAvg

【讨论】:

    【解决方案2】:

    你可以这样做,这里是demo

    select  
        first_name,
        salary,
        department_id
    from
    (select
        department_id,
        first_name,
        salary,
        avg(salary) over () as avg_salary
    from employee
    ) emp
    where salary > avg_salary
    

    输出:

    +--------------------------------+
    first_name  salary  department_id|
    +--------------------------------+
    | Ian       80000      2         |
    | John      50000      3         |
    +--------------------------------+
    

    【讨论】:

      【解决方案3】:

      窗口函数可能是最好的解决方案——就像@zealous 的回答一样。不过,子查询也是很合理的。这个想法是:

      select e.first_name, e.salary, e.department_id
      from employees as e
      where e.salary > (select avg(e2.salary)
                        from employees e2
                        where e2.department_id = e.department_id
                       );
      

      Here 是一个 dbfiddle。

      关键思想:

      • 您无需将join 转至departments。仅仅因为你有这个想法并不意味着join 是必要的。
      • GROUP BY 在子查询中是不必要的。相关性子句负责这一点。

      【讨论】:

      • 像这样,但在我的环境中只返回一行。
      • @mr-sk 。 . .当然。您的样本数据包含三个部门,其中只有一名员工。该员工的平均工资不能高于部门平均水平——该员工的工资就是部门平均工资。
      猜你喜欢
      • 2019-01-25
      • 2011-04-28
      • 1970-01-01
      • 2022-11-10
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 2021-09-09
      相关资源
      最近更新 更多