SQL 如何找到所有部门的总工资

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41280637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 05:03:40  来源:igfitidea点击:

How to find total salary of all departments

sql

提问by M Yasir Latif Kassar

SELECT D_ID, SUM(SALARY) 
FROM EMPLOYEE 
GROUP BY D_ID.

I want to get total salary of all departments with this query.

我想通过这个查询获得所有部门的总工资。

回答by Jibin Balachandran

If department wise sum then your query is proper.

如果部门明智的总和,那么您的查询是正确的。

SELECT D_ID, SUM(SALARY) 
FROM EMPLOYEE 
GROUP BY D_ID

If you want the total sum then can do a sumof salary.

如果你想要总金额,那么可以做一个sum工资。

SELECT SUM(SALARY) 
FROM EMPLOYEE 

If you want to display both a single query then use:

如果要同时显示单个查询,请使用:

SELECT D_ID, 
       SUM(SALARY) OVER (PARTITION BY D_ID) AS [Dept Salary], 
       SUM(SALARY) OVER () AS [Total Salary] 
FROM EMPLOYEE 

回答by Dinesh Singh

Ideal query should be as below:

理想的查询应如下所示:

SELECT D.D_ID, SUM(E.SALARY) 
FROM DEPARTMENT D
LEFT JOIN EMPLOYEE E ON D.D_ID=E.D_ID
GROUP BY D.D_ID
UNION 
SELECT 0 D_ID, SUM(SALARY) SALARY
FROM EMPLOYEE