I have a situation where I need to determine whether or not a record exists where a start datetime and end datetime overlap the start datetime and end datetime of the record.
I have tblEmployees, tblShifts, and tblShiftAssignments. tblShiftAssignments is nothing more than a shift ID and an employee ID. tblShifts has all of the shift data and tblEmployees has all of the employee data.
I need to get a list of all employees that are not assigned to a shift during a specified time frame. For example, lets say we have a shift for 07/19/2013 08:00:00 to 07/19/2013 17:00:00. What I need to do is find every employee that isn't working at all during that time frame. If their shift starts at 16:00:00 then they need to be excluded. If it ends at 08:30:00 they need to be excluded. So to that end, I have come up with this:
I've loooked this over time and again and something doesn't quite seem right.
The questions I have are:
Will this return an employee record if no records exists in tblShiftAssignments?
Will this work as I expect and filter out all employees who are already scheduled to work at any time between @ShiftBegin and @ShiftEnd?
At the moment, I don't have the ability to test it.
I have tblEmployees, tblShifts, and tblShiftAssignments. tblShiftAssignments is nothing more than a shift ID and an employee ID. tblShifts has all of the shift data and tblEmployees has all of the employee data.
I need to get a list of all employees that are not assigned to a shift during a specified time frame. For example, lets say we have a shift for 07/19/2013 08:00:00 to 07/19/2013 17:00:00. What I need to do is find every employee that isn't working at all during that time frame. If their shift starts at 16:00:00 then they need to be excluded. If it ends at 08:30:00 they need to be excluded. So to that end, I have come up with this:
VB.NET:
@ShiftBegin datetime,
@ShiftEnd datetime
AS
SELECT e.* FROM tblEmployees e
WHERE (SELECT COUNT(*) FROM tblShifts sh
INNER JOIN tblShiftAssignments sa
ON sa.SID = sh.ID
WHERE @ShiftBegin >= sh.ShiftEnd OR @ShiftEnd <= sh.ShiftBegin -- <---------this is what I am having trouble reconciling in my mind
AND sa.EID = e.ID) = 0
RETURN
I've loooked this over time and again and something doesn't quite seem right.
The questions I have are:
Will this return an employee record if no records exists in tblShiftAssignments?
Will this work as I expect and filter out all employees who are already scheduled to work at any time between @ShiftBegin and @ShiftEnd?
At the moment, I don't have the ability to test it.