如何计算每个工资和最低工资之间的差额

时间:2014-11-12 07:34:40

标签: sql-server

我使用的是sql server 2008 rs1,我的查询有问题。 我的桌子名字是:员工 表列是: first name , last name , job title , gender , date of birth and salary

必填:显示职位,员工姓名,以及员工薪水与工作最低工资之间的差额

我试着解决它:

SELECT First_Name, Title ,Salary-min(Salary) AS (Differance) FROM Employees;

但这会导致错误 请帮帮我 提前谢谢

5 个答案:

答案 0 :(得分:1)

假设最低工资是所有员工的最低工资,您需要从所有员工中选择最低工资并使用;

SELECT 
  First_Name, 
  Title, 
  Salary - (SELECT MIN(SALARY) FROM Employees) As Difference 
FROM Employees

答案 1 :(得分:0)

试试这个。

;WITH cte
     AS (SELECT Min(Salary) Differance
         FROM   Employees)
SELECT First_Name,
       Title,
       Salary - (SELECT * FROM   cte) AS Differance
FROM   Employees; 

答案 2 :(得分:0)

您可以使用子查询来计算最低工资:

SELECT First_Name, Title ,Salary-(SELECT MIN(e2.Salary)
                                  FROM Employees e2) AS Difference 
FROM Employees

您不必担心每行计算一次。

答案 3 :(得分:0)

试试这个

;WITH cte(minimumSalray)
AS
(
    SELECT MIN(Salary) 
    FROM Employees
)
SELECT First_Name, Title ,Salary-cte.minimumSalray AS Differance 
FROM Employees,minimumSalary

答案 4 :(得分:0)

没有SQL Server 2008 R2,只是在SQL Server 2014上试过。

SELECT First_Name, Title ,
    Salary-min(Salary) OVER() AS [Differance] 
FROM Employees;

如果您想获得同一工作的最低工资,只需查询

SELECT First_Name, Title ,
    Salary-min(Salary) OVER(PARTITION BY Title) AS [Differance] 
FROM Employees;