我试图让这个查询起作用。基本上,所有没有牛津布鲁克斯学位的人现在的薪水都高于整个数据库的平均工资。 我已经搜索了几个小时试图找到解决方案。请随时提供建议,以便我可以从中学习并解决我的问题。感谢
SELECT a.personid,
a.firstname,
a.lastname,
b.placeofstudy,
AVG(c.salary)
FROM person a
INNER JOIN award d
ON a.personid = d.personid
INNER JOIN qualification b
ON d.qualid = b.qualid
INNER JOIN job c
ON a.personid = c.personid
WHERE placeofstudy = 'Oxford Brookes'
GROUP BY a.personid;
这目前没有产生任何错误,但没有返回平均工资,只是每个人当前的工资。即使我添加'有c.salary> avg(c.salary)'它拒绝工作。
答案 0 :(得分:2)
select a.personid, a.firstname, a.lastname, b.placeofstudy, c.salary, (select avg(salary) from job) as AvgSalary
from person a
inner join award d on a.personid = d.personid
inner join qualification b on d.qualid = b.qualid
inner join job c on a.personid = c.personid
where placeofstudy = 'Oxford Brookes'
and c.salary > (select avg(salary) from job)
答案 1 :(得分:1)
您需要使用关键字HAVING。
试试这个:
select a.personid, a.firstname, a.lastname, b.placeofstudy, avg(c.salary) AS avgsalary
from person a
inner join award d
on a.personid=d.personid
inner join qualification b
on d.qualid=b.qualid
inner join job c
on a.personid=c.personid
where placeofstudy = 'Oxford Brookes'
group by a.personid, a.firstname, a.lastname, b.placeofstudy
having c.salary > avgsalary