我正在学习数据库,我对查询有疑问:
我如何锻炼平均工资,包括总工资和工资差异?
我的表格如下:
Staff: staffNo(PK), staffSalary
我写的查询是
SELECT staffNo, staffSalary,staffSalary - (
SELECT AVG( staffSalary )
FROM Staff ) AS salDiff
FROM Staff
WHERE staffSalary > (
SELECT AVG( staffSalary )
FROM Staff )
此查询仅提供平均薪资差额和员工实际薪资,如何找出每个数据列的实际薪资平均值? 在这个例子中也可以使用连接吗?
答案 0 :(得分:0)
试试这个
SELECT staffNo, staffSalary,staffSalary - (SELECT AVG(staffSalary) FROM Staff ) AS salDiff,
(SELECT AVG(staffSalary) FROM Staff
WHERE staffSalary > (SELECT AVG(staffSalary) FROM Staff )) as avSalForStaffGreaterThanAverageSal
FROM Staff
WHERE staffSalary > (SELECT AVG(staffSalary) FROM Staff )
答案 1 :(得分:0)
假设每位员工有一排,这就是我相信你要求的(对于所有高于平均水平的员工):
select staffNo,
(Select avg(staffsalary) from staff) as avgSalary,
(select staffSalary from staff where staffNo = S.StaffNo) as Salary,
(Select staffSalary - (select avg(staffSalary) from staff) from staff where staffNo = S.staffNo ) as salDiff
from staff S
where staffSalary > (Select avg(staffSalary) from staff)
不确定为什么你需要加入...除非你想看到所有用户的工资,在这种情况下你可以简单地删除where子句,它会告诉你所有的工作人员。