Hi,
iam working in an employee management application in which i want to find out intime, outtime, overtime of each employee in a day.
following is working code
create table #temp(empid numeric(18,0),empname nvarchar(50),intime datetime,outtime datetime)
insert into #temp values(2500,'Sachin','2014-01-01 09:00:00','2014-01-01 10:30:00:00')
insert into #temp values(2500,'Sachin','2014-01-01 11:00:00','2014-01-01 12:30:00:00')
insert into #temp values(2500,'Sachin','2014-01-01 13:30:00','2014-01-01 20:30:00:00')
select EmpId ,CONVERT(VARCHAR(20), InTime,106 ) as workingday,left(CONVERT(TIME,MIN(InTime),108),5)as intime,left(CONVERT(TIME,max(outTime),108),5)as outtime ,dstatus=
(CASE
WHEN CONVERT(TIME,MIN(InTime),108)>'08:35' and CONVERT(TIME,MIN(InTime),108) <'11:00'
THEN 'L'
WHEN CONVERT(TIME,MIN(InTime),108)>'11:00'
THEN 'halfday'
else 'right' end ),convert(varchar(5), max(OutTime)-MIN(InTime),108) as Difference
,RIGHT('0'+CONVERT(varchar, FLOOR(SUM(DATEDIFF(MI, inTime, outTime)/60.0))) , 2)
+':'+RIGHT('0'+CONVERT(varchar, SUM(DATEDIFF(MI, inTime, outTime))%60), 2) AS [total work_hour(s)]
,left(cast(CAST(DATEADD(MINUTE, SUM(DATEDIFF(MI, inTime, outTime))-45, 0) AS TIME) as varchar),5) TotalTime,
left(cast(CAST(DATEADD(MINUTE, CASE WHEN SUM(DATEDIFF(MI, inTime, outTime)) > 525 THEN SUM(DATEDIFF(MI, inTime, outTime))-525 ELSE 0 END, 0 ) AS TIME ) as varchar),5) OverTime,
right('0'+CAST((datediff(mi,CAST(DATEADD(MINUTE, CASE WHEN SUM(DATEDIFF(MI, inTime, outTime)) > 525 THEN SUM(DATEDIFF(MI, inTime, outTime))-525 ELSE 0 END, 5 ) AS TIME ),CAST(DATEADD(MINUTE, SUM(DATEDIFF(MI, inTime, outTime))-45, 0) AS TIME)) / 60) AS VARCHAR(8)),3)
+':'+right('0'+CAST((datediff(mi,CAST(DATEADD(MINUTE, CASE WHEN SUM(DATEDIFF(MI, inTime, outTime)) > 525 THEN SUM(DATEDIFF(MI, inTime, outTime))-525 ELSE 0 END, 0 ) AS TIME ),CAST(DATEADD(MINUTE, SUM(DATEDIFF(MI, inTime, outTime))-45, 0) AS TIME)) % 60) AS VARCHAR(2)), 2) ActualTime
FROM #temp GROUP BY EmpId , CONVERT(VARCHAR(20), InTime,106 )
drop table #temp
its output is
EmpId workingday intime outtime dstatus Difference total work_hour(s) TotalTime OverTime ActualTime
2500 01 Jan 2014 09:00 20:30 L 11:30 10:00 09:15 01:15 08:00
this is working fine.
following is calcultion of each field
difference means difference of maximum outtime-minimum intime
total work_hour(s) means Diffrence- sparetime
here an employee with id 2500 arrives at 09:00 and gone out side at 10:30 again come at 11:00 here he spends 30 minutes outside.there is total 01:30 spare time in this table
Totaltime means total work_hour(s)-45(lunch time)
Actual time means it is fixed 8 hour a day
overtime means the time exceeds 08 hrs.
if total time is more than 08 hrs overtime =Totaltime-actualtime else overtime is 0.
here my requirement is lunch time is from 12:45 to 13:30 .if anyone go outside on this time this shouldn't be cakculated.in this table the employee gone outside at 10:30 and come back at 11:00 here spare time is 00:30 hrs.
he again gone outside at 12:30and come back at 13:30 .here is the error according to query ,the spare time is 01:00 hrs.
but my requirement is it should be 15 minutes means from 12:30 to 12:45 . because 12:45 to 13:30 is lunch time.
suppose any gone outside at 12:30 and come back at 13:45 sparetime should be 15+15=00:30 hrs.
finally my output should be like this
EmpId workingday intime outtime dstatus Difference total work_hour(s) TotalTime OverTime ActualTime
2500 01 Jan 2014 09:00 20:30 L