문제

I have a table of employees Salary named as empSalary.

empSalary

I want to calculate sum of salaries issued by each department. What comes to my mind is

Select sum(Salary) from empSalary where deptId = (Select deptId from empSalary)

This statement gives me 5100 which is the sum of Salary where deptId = 1. How is this possible using only sql query? Sorry for the question title as i was unable to find words.

도움이 되었습니까?

해결책

You want to GROUP BY the Department:

SELECT Dept_ID
      ,sum(Salary) Total_Salary
FROM empSalary
GROUP BY Dept_ID

다른 팁

This is exact use of group by , you make groups and and each group you can calculate sum, average, min , max or count

select DeptID, Sum(Salary) as SumSalary
From empSalary
Group by DeptID

If I understood the question correctly, "group by" in SQL helps here.

      Select sum(Salary) from empSalary group by DeptID;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top