SQL - Calculate hours from DateTime

Hi,

I have the following SQL query:

Select DISTINCT Emp.Name + ' '+ Emp.Surname As FullName, RC.ClockIn,
IO.Name, Present = CASE Dir.InOrOut WHEN 'IN' THEN 'Y' WHEN 'OUT' THEN 'N/A' END
FROM Employees Emp, ClockIns RC, InOrOut IO
WHERE Emp.Emp_Id = RC.Emp_ID
AND IO.IO_Id = RC.IO_ID
AND convert(varchar(10), RC.ClockIn, 111) >= '2010/10/20'
AND convert(varchar(10), RC.ClockIn, 111) <= '2010/10/27'

The result is displayed as follows:

FullName              ClockIn                                  Name              Present
Person1               2010-01-05 08:30:30            IN                       Y
Person1               2010-01-05 10:00:00            IN                       Y
Person1               2010-01-05 11:23:21            IN                       Y
Person1               2010-01-05  17:00:00           OUT                   N/A
Person2               2010-01-05  07:58:10          IN                       Y
Person2               2010-01-05 12:15:48           IN                        Y
Person3               2010-01-05  16:50:00          OUT                    N/A

I want to write a query to calculate how many hours a person has been at work.  As you can see with the results, I need to find the first time the person logged in, and then find the las time the person logged out, and calculate the difference in time.

any ideas on how to do this?
NerishaBAsked:
Who is Participating?
 
RGBDartCommented:
If first ClockIn for a person is StartTime, and last Clockin for a person is EndTime (during a single day), your query will look like this:

select
	Emp.Name + ' '+ Emp.Surname As FullName,
	MIN(RC.ClockIn) as StartWorkTime,
	MAX(RC.ClockIn) as EndWorkTime,
	cast(datediff(mi, MIN(RC.ClockIn), MAX(RC.ClockIn)) as numeric(12,4)) / 60.0 as ShiftLenghtHours
from Employees Emp, ClockIns RC, InOrOut IO 
where
	Emp.Emp_Id = RC.Emp_ID 
	AND IO.IO_Id = RC.IO_ID 
	AND convert(varchar(10), RC.ClockIn, 111) >= '2010/10/20' 
	AND convert(varchar(10), RC.ClockIn, 111) <= '2010/10/27'
group by Emp.Name + ' '+ Emp.Surname,
	DATEPART(yyyy, RC.ClockIn),
	DATEPART(mm, RC.ClockIn),
	DATEPART(dd, RC.ClockIn)

Open in new window


0
 
Paul_Harris_FusionCommented:
Assuming start and end times are on the same day it is pretty easy

Select DateDiff(hour, startTime, endTime)

http://msdn.microsoft.com/en-us/library/aa258269(v=sql.80).aspx

If you can span different days, you need to do a little owrk in your query to account for tis correctly
0
 
NerishaBAuthor Commented:
But there is a problem here, I dont know the "Start" and "End" date.  Both the Time in and Time Out are stored in the same field.  I need to somehow find out the first clockIn for the day, and the last clock out to become my StartTime and EndTime.  This will be for the same day.
0
 
SharathData EngineerCommented:
Do you have more than one "OUT" records for a person?
How do you want your expected result to be displayed?
Person2 does not have OUT time, what would be the no. of hours worked for this person?
Person3 does not have IN time, Is this valid? what would be the no. of hours worked for this person?
0
 
NerishaBAuthor Commented:
Sometimes a person logs in and forgets to log out,  in this case, we would like to use a default Start Time and end Time.  The result that RGBDart works fine for now. Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.