Calculate each day working hours by employee

One of my friend asked me to calculate working hours and break hours of each employee in each day. The table maintained in below format as tabulated. So I needed aggregate and then transpose the values.


I found solution with below script

select EmpID,
  max(case when EmpAct = 'Production' and WorkDate = '2014-05-13 00:00:00.000' then TimeConsumed end) May13_Work,
  max(case when EmpAct = 'Break' and WorkDate = '2014-05-13 00:00:00.000' then TimeConsumed end) May13_Break,
  max(case when EmpAct = 'Production' and WorkDate = '2014-05-14 00:00:00.000' then TimeConsumed end) May14_Work,
  max(case when EmpAct = 'Break' and WorkDate = '2014-05-14 00:00:00.000' then TimeConsumed end) May14_Break
  from
(
SELECT EmpID,EmpAct,DATEADD(dd, 0, DATEDIFF(dd, 0, InTime)) As WorkDate,
Sum(DATEDIFF(MINUTE,InTime,Outtime)) AS TimeConsumed from test Group by EmpID,EmpAct,DATEADD(dd, 0, DATEDIFF(dd, 0, InTime))
) src
Group by EmpID

No comments:

Post a Comment