如何查找所有部门的总薪水

时间:2016-12-22 10:11:41

标签: sql

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

我希望通过此查询获得所有部门的总薪水。

3 个答案:

答案 0 :(得分:1)

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

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

如果你想要总和,则可以获得sum的工资。

SELECT SUM(SALARY) 
FROM EMPLOYEE 

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

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

答案 1 :(得分:0)

理想的查询应如下所示:

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

答案 2 :(得分:0)

从 OP 的评论来看,我认为他是在要求这个。

mysql> select * from xyz;
+------+------------+---------------+--------+------------+
| roll | name       | location      | salary | department |
+------+------------+---------------+--------+------------+
|    1 | Pranav     | Parasi        | 25000  | A          |
|    2 | Pragya     | Chitwan       | 20000  | A          |
|    3 | Prabhuddha | New Playmouth | 28000  | B          |
|    4 | Smriti     | Kathmandu     | 35000  | B          |
|    5 | Shruti     | Butwal        | 40000  | A          |
+------+------------+---------------+--------+------------+
5 rows in set (0.00 sec)

mysql> select department, sum(salary) from xyz group by department;
+------------+-------------+
| department | sum(salary) |
+------------+-------------+
| A          |       85000 |
| B          |       63000 |
+------------+-------------+
2 rows in set (0.01 sec)

通过这样做,我们可以在一列中显示部门,在另一列中显示部门的薪水。