Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I have one table and multiple columns my table data like this
tblEmployeeTimeCard

RecID |EmployeeID|clientID |FromTime              |ToTime                 |AutoDetect 
1     |  1       |1        |2013-04-05 08:10:00AM |2013 -04-05 12:00:00PM |1
2     |  1       |1        |2013-04-05 13:00:00PM |2013-04-05 19:10:00PM  |1
3     |  1       |1        |2013-04-05 20:00:00PM |2013-04-05 23:45:00 pM |1


the above rows are single Employee work stats report. my question is
1)how to get the total(SUM) working hours in particular day.
2)Employee works above 6hrs in particular Day then Every 6hr detect the 30 mints form total hours
(i.e- Subtract the detect mints form (sum)total working hrs when autoDetect= 1)

how to write the query for get the total working hours with in the single row.

please help me..

Thanks and Regards
Posted
Updated 20-May-13 10:19am
v2

1 solution

Hi,

check the following Code
SQL
-- Question 1 Answer
SELECT E.EmployeeID,E.Day, (E.TotalTime/60) 'TotalHours', (E.TotalTime%60) 'Minites'
FROM (SELECT EmployeeID,CONVERT(VARCHAR(10),FromTime,102) 'Day',SUM(DATEDIFF(mi,FromTime,ToTime)) 'TotalTime'
      FROM tblEmployeeTimeCard
      GROUP BY EmployeeID,CONVERT(VARCHAR(10),FromTime,102))E

-- Question 1&2 Answer
SELECT E.EmployeeID,E.Day, (E.TotalTime/60) 'TotalHours', (E.TotalTime%60) 'Minites',
CASE WHEN (E.TotalTime/60)>=6 THEN E.TotalTime - (30* ROUND((E.TotalTime/60*6),1))
     ELSE (E.TotalTime/60) END 'TotalWorkHours'
FROM (SELECT EmployeeID,CONVERT(VARCHAR(10),FromTime,102) 'Day',SUM(DATEDIFF(mi,FromTime,ToTime)) 'TotalTime'
      FROM tblEmployeeTimeCard
      GROUP BY EmployeeID,CONVERT(VARCHAR(10),FromTime,102))E


For ur Question 2, I am not getting. give some more details about that. if possible send some sample data. if access over lap like (From date is 2013 -04-05 11:55:00PM to 2013-04-05 19:10:00PM)
then we can't able to fine the time difference. So check the code for Question No 1 and give the feedback.

Regards,
GVPrabu
 
Share this answer
 
v2
Comments
Maciej Los 20-May-13 16:35pm    
It should works!
+5
Santhosh23 21-May-13 3:00am    
hi prabu..
thanks for replay
your query is working fine. thank you so much...
Santhosh23 21-May-13 3:06am    
my second questions is how to detect the hours from TotalHours. i.e
employee working 14hours in single day. so my question is..
after every 6 hours subtract the 30 mints from TotalHours. employee worked on 14hrs in the Day. so Subtract the 60 mints in total hours. how to write the query for subtract detect hours and get the reaming hours..?
please help me prabhu..
thanks
gvprabu 21-May-13 3:22am    
ok fine, I ll check and get back to u soon :-)
gvprabu 21-May-13 3:36am    
hi,
Check my solution, I updated. I can't able to check this bcos in my machine I don't have SQL Server. :-) :-) :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900