Calculate sum of working hours in sql server and display in each row regarding the EmpID
I want to calculate SumofWorkingHours as below which is based in empID. e.g. if EmpID is 1 then it sum the working hours in every Admincode and display sum in all rows where EmpID exist. Please give suggestion or query regarding thi开发者_运维百科s.
LoginName EmpID Practice AdminCode WorkingHours SumofWorkingHours E1 1 Admin IT 5.24 38.81 E1 1 Admin Finance 0.07 38.81 E1 1 Admin HR 33.50 38.81 E2 2 Admin Support 8.00 40.50 E2 2 Admin HR 32.50 40.50 E3 3 Admin Finance 5.50 45.08 E3 3 Admin Support 39.58 45.08
This data is only for testing
Looks like you just want to sum over EmpId, like this:
select EmpId, sum(WorkingHours)
from theTable
group by EmpId
To integrate that into a larger query to specifically return the result set that you show in your question, you could do something like:
select t1.LoginName, t1.EmpID, t1.Practice,
t1.AdminCode, t1.WorkingHours, tot.SumofWorkingHours
from theTable t1
inner join (select EmpID, sum(WorkingHours) as SumofWorkingHours
from theTable
group by EmpId) tot
on tot.EmpID = t1.EmpID
精彩评论