我怎样才能对我的结果sumOfSalary进行顶部查询

时间:2018-08-23 08:20:22

标签: mysql sql

SELECT 
FORMAT(SUM(e.salary),0) as sumOfSalary, 
d.departName 
FROM
employee e 
JOIN depart d on (d.id = e.depart) 
GROUP BY d.departName
ORDER BY sumOfSalary desc

如何在不使用LIMIT 1的情况下从sumOfSalary返回最高值的结果?我试图在MAX()中使用SUM(e.salary),但是这并没有帮助我收到错误#1111-组功能的无效使用

3 个答案:

答案 0 :(得分:1)

使用LIMIT只返回查询的最高结果:

SELECT 
    FORMAT(SUM(e.salary),0) as sumOfSalary, 
    d.departName 
FROM
    employee e 
JOIN depart d 
    on (d.id = e.depart) 
GROUP BY d.departName
ORDER BY sumOfSalary desc
LIMIT 1

答案 1 :(得分:0)

如果您只需要所有部门的最高薪水(我希望我能正确回答您的问题!),请使用嵌套查询:

SELECT FORMAT(MAX(sumOfSalary),0) FROM (
SELECT 
SUM(e.salary) as sumOfSalary, d.departName 
FROM employee e 
JOIN depart d on d.id = e.depart)
GROUP BY d.departName
) AS ss

答案 2 :(得分:0)

使用格式时,结果不是按数值排序,而是按字母顺序排序 因此,“ 600”按降序排在“ 3000”之前

    select departName, format(max(sumOfSalary),0) from 
 (SELECT 
    SUM(e.salary) as sumOfSalary, 
    d.departName 
    FROM
    employee e 
    JOIN depart d on (d.id = e.depart) 
    GROUP BY d.departName
    ORDER BY SUM(e.salary) desc limit 1) as nested