Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I have a access recording table data in the sql table i need to calculate number of hours is in office and also i need to calculate the number of hours he statyed out.
Any one can use cursor for this and give query
e,g The below table gives detail of the table of one employee.
DoorNo 1 is entry door code 5 is exit door code
Recordedtime is time accessed
accesscardNo is the card No of Employee,
EntryExit means A001 is Entry A0002 is Exit from office
Slno   DoorNo   	RecordedTime        AccessCardNo EntryExit
1	1	2011-09-08 09:23:37.000 	0050	   A0001 
1	5	2011-09-08 12:11:34.000	        0050	   A0002 
1	1	2011-09-08 13:49:59.000	        0050	   A0001 
1	5	2011-09-08 14:44:32.000	        0050	   A0002 
1	1	2011-09-08 14:46:43.000	        0050	   A0001 
1	5	2011-09-08 17:46:31.000	        0050	   A0002 
1	1	2011-09-08 17:47:32.000	        0050	   A0001 
1	5	2011-09-08 17:52:48.000	        0050	   A0002 
1	1	2011-09-08 17:58:18.000	        0050	   A0001 
1	5	2011-09-08 18:49:10.000	        0050	   A0002 


Thanks in advance
Posted
Updated 22-Sep-11 0:51am
v3
Comments
_Damian S_ 22-Sep-11 1:36am    
How are you handling the instance where he has entered twice but not exited between? ie: Rows 3 and 4.
Briju.metallica 22-Sep-11 1:44am    
This is raw data got from the access recorder , some times employee may aceess sometimes not i have to handle that also what to do
_Damian S_ 22-Sep-11 2:00am    
The point I am making is that you need to have a business rule that you can consistently apply to handle these situations. Without that rule to apply, you can't proceed past that point.
Thiagarajan Duraisamy 22-Sep-11 2:25am    
In case of the case here, he can take the last entry time and first exit time between multiple entries without recording and multiple exits without recording.
and can go for a stored proc to calculate tat.

1 solution

Hi,
Follow the below steps:
1. Remove all blank spaces from AccessCardNo and EntryExit field (if any blank space)
Note: I just copy your provided table value and got blank space so remove them.

2. Execute the below query, it will give you Total Seconds stayed in office of a particular employee

SQL
select datediff(s,(SELECT TOP 1 RecordedTime AS Entry FROM tblTest Where AccessCardNo='0050' AND EntryExit='A0001'
ORDER BY RecordedTime ASC),(SELECT TOP 1 RecordedTime AS Out FROM tblTest Where AccessCardNo='0050' AND EntryExit='A0002'
ORDER BY RecordedTime DESC)) AS TotalSecondsInOffice


3. Keep the value in a variable

4. If you divide the result by 3600 then you will get hour
5. If you need Minute then divide the Remainder value by 60 then you will get Minute

Note: If you need only hour then run the below query
SQL
SELECT DATEDIFF(s,(SELECT TOP 1 RecordedTime AS Entry FROM tblTest Where AccessCardNo='0050' AND EntryExit='A0001'
ORDER BY RecordedTime ASC),(SELECT TOP 1 RecordedTime AS Out FROM tblTest Where AccessCardNo='0050' AND EntryExit='A0002'
ORDER BY RecordedTime DESC))/3600 AS TotalHoursInOffice




Thanks,
Mamun
 
Share this answer
 
Comments
Briju.metallica 23-Sep-11 0:26am    
Thanks for the Soln and also need every interval of the time. How many hours he stayed in office and how many hours stayed out of office

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