查询:计算飞行员平均工资与员工(包括飞行员)平均工资之间的差额。 飞行员是那些出现在经过认证的表格中的员工
查询我写道:
select (select avg(salary) from
employee join certified using (eid))-(select avg(salary) from employee) as Difference;
但是它给出了错误的结果,我认为通过使用认证表中的每个条目都是结果平均值。(eid 10在认证中出现4次,因此它使用员工的工资和eid 10四次),我需要计算仅在认证表中的不同eid的平均值。
怎么做?
答案 0 :(得分:1)
试试这个:
select (select avg(salary)
from employee
where eid in (select eid from certified))
- (select avg(salary) from employee) as Difference;
或加入:
select (select avg(salary)
from employee
join (select distinct eid from certified) as t
using (eid))
- (select avg(salary) from employee) as Difference;