列出在超过 2 名员工的团队中从平均收入中获得平均值(准确率高达 30%)的员工(姓名、base_salary)
Posted
技术标签:
【中文标题】列出在超过 2 名员工的团队中从平均收入中获得平均值(准确率高达 30%)的员工(姓名、base_salary)【英文标题】:List the employees (name, base_salary) that earn an average value (with accuracy up to 30%) from the average earnings in teams of >2 employees 【发布时间】:2020-06-18 16:44:41 【问题描述】:这是我目前得到的:
SELECT surname, base_salary from emp p LEFT JOIN (select id_team, avg(base_salary) as s, count(*) as c from emp group by id_team) as o ON(p.id_team = o.id_team)
where p.base_salary between o.s*0.7 and o.s*1.3 and o.c >=2
在 Oracle LIVE SQL 上,我收到 ORA-00905:缺少关键字错误。
这是桌子的样子。
【问题讨论】:
【参考方案1】:您的问题是使用as
关键字为子查询设置别名。甲骨文不支持。您的查询应该可以删除它:
select ...
from emp p
left join (
select id_team, avg(base_salary) as s, count(*) as c from emp group by id_team
) as o on p.id_team = o.id_team)
--^-- here
where ...
另一方面,我认为使用窗口函数可以更有效地表达您的查询:
select surname, base_salary
from (
select
surname,
base_salary,
avg(base_salary) over(partition by id_team) avg_base_salary,
count(*) over(partition by id_team) no_emps
from emp
) e
where no_emps > 2 and base_salary between avg_base_salary * 0.7 and avg_base_salary * 1.3
【讨论】:
非常感谢!以上是关于列出在超过 2 名员工的团队中从平均收入中获得平均值(准确率高达 30%)的员工(姓名、base_salary)的主要内容,如果未能解决你的问题,请参考以下文章
如何在 MySQL 中汇总表以获得基于几列的 3 个月平均收入?