我有一组员工和一组时隙。例如
Employees
EMP1
EMP2
EMP3
EMP4
EMP5
Time Slots
08:00AM-10:00AM
10:00AM-12:00PM
04:00PM-06:00PM
08:00AM-12:00PM
02:00PM-06:00PM
08:00AM-04:00PM
09:00AM-01:00PM
08:30AM-01:30PM
12:00PM-04:00PM
2:00PM-3:00PM
可以将员工分配到任何时隙。我需要的是,如果某个员工在8:00Am-12:00PM的时间段分配,那么该员工应该在08:00 AM-10:00AM和10:00 AM-12:00PM的时间段不可用。同样,如果在04:00 PM-06:00PM分配了一个员工,则该员工在04:00 PM-06:00PM之间的任何时间段都不应该有空,但可以在该时间段之前和之后(而不是在两个时间段之间)使用。
该怎么做?
答案 0 :(得分:-1)
我假设您有一个映射员工和时隙的桥表。我可以通过以下开发的方案来开发解决方案
SELECT * INTO dbo.Employees FROM (
SELECT 1 empid,'EMP1' emp
union
SELECT 2,'EMP2'
UNION
SELECT 3,'EMP3'
UNION
SELECT 4,'EMP4'
UNION
SELECT 5,'EMP5' )a
SELECT * INTO dbo.TimeSlots FROM (
SELECT 1 sltid,'08:00AM-10:00AM' slt
UNION
SELECT 2,'10:00AM-12:00PM'
UNION
SELECT 3,'04:00PM-06:00PM'
UNION
SELECT 4,'08:00AM-12:00PM'
UNION
SELECT 5,'02:00PM-06:00PM'
UNION
SELECT 6,'08:00AM-04:00PM'
UNION
SELECT 7,'09:00AM-01:00PM'
UNION
SELECT 8,'08:30AM-01:30PM'
UNION
SELECT 9,'12:00PM-04:00PM'
UNION
SELECT 10,'2:00PM-3:00PM')a
SELECT * INTO dbo.TimeSlotsemp FROM (
SELECT 1 emp,2 slt
UNION
SELECT 1,3
UNION
SELECT 2,6)a
--available slots
SELECT DISTINCT a.empid,a.emp,d.slt AS availableslots FROM employees a
cross JOIN timeslots d
LEFT JOIN TimeSlotsemp b ON a.empid = b.emp
left JOIN timeslots c
ON b.slt = c.sltid
WHERE (d.sltid NOT IN (SELECT slt FROM timeslotsemp WHERE emp = a.empid) or a.empid NOT IN (SELECT emp FROM timeslotsemp))