如何解决这个查询找到差异?

时间:2016-07-17 05:00:14

标签: sql oracle

我与(emp_id,emp_name,salary)等属性有员工关系,我想找到每个员工的薪水和平均工资之间的差异。我正在使用SQL Plus。

即。每个salary-avg(salary)

2 个答案:

答案 0 :(得分:0)

试试这个。

SELECT a.emp_id,
       a.emp_name,
       a.salary,
       a.Designation,
      (a.salary - b.salary) as salary_difference 
FROM employee AS a
INNER JOIN (
  SELECT Designation, avg(salary) as salary
  from employee
  group by Designation) as b
ON a.Designation = b.Designation ;

SQL Fiddle HERE

答案 1 :(得分:0)

这可以使用窗口函数来完成:

SELECT emp_id,
       emp_name,
       salary
       salary - avg(salary) over () as diff
FROM employee