计算所有员工的最高工资

时间:2016-08-05 01:45:02

标签: sql sql-server sql-server-2012

我有一个名为EMPLOYEE的表,有17个不同的列。一列称为Salary,它是显示所有员工薪水的列。我想计算最高工资,我知道这不是正确的答案。该列中的最高薪水是131250,随着该报表,它继续拉高该值。不知道为什么它没有计算所有员工。

SELECT MAX(CAST(Salary as MONEY))AS Highest_Salary FROM Employee

结果= 131250

首先数据类型是varchar,然后我将其更改为money而下一个int仍然不好。可能是什么问题?

6 个答案:

答案 0 :(得分:1)

尝试使用

SELECT MAX(CAST(薪水为DECIMAL(12,2)))AS Highest_Salary FROM Employee

答案 1 :(得分:1)

您可能打算放SELECT MAX(CAST(Salary as MONEY)) AS Highest_Salary FROM Employee WHERE Salary = X但是老实说找到最大值,您不希望涉及WHERE条款,所以只需删除= 131250

答案 2 :(得分:1)

您是否尝试删除此部分' = 131250'并尝试查看输出??

答案 3 :(得分:0)

SELECT MAX(转换(金钱,薪水))AS Highest_Salary FROM Employee

答案 4 :(得分:0)

你想找到最高工资或平均工资吗?

最高工资

SELECT MAX(CAST(Salary as MONEY)) AS Highest_Salary FROM Employee

平均工资

SELECT AVG(CAST(Salary as MONEY)) AS Highest_Salary FROM Employee

答案 5 :(得分:0)

如果您想要每位员工的最高工资,可以使用GROUP BY

SELECT CAST(MAX(Salary) as MONEY) AS Highest_Salary
FROM Employee
GROUP BY EmployeeID

如果您希望获得可以使用应用程序的平均值,或者运行此其他查询。

SELECT CAST(AVERAGE(Highest_Salary) as MONEY) as avg_salary
FROM (
  SELECT CAST(MAX(Salary) as MONEY) AS Highest_Salary
  FROM Employee
  GROUP BY EmployeeID
) high_salaries