通过比较表两工作时间,在表1中找到空闲时间

时间:2013-09-03 11:33:01

标签: sql sql-server sql-server-2008-r2

create table timetable( work_date date, tech_id int,
    block_1_firsthalf_start_time datetime,  block_1_end_time datetime,
    block_2_secondhalf_start_time datetime, block_2_end_time datetime,
    block_3_over_time datetime, block_3_over_end_time datetime,
    block_4_first_break_start_time datetime,    block_4_first_break_end_time datetime,
    block_5_second_break_start_time datetime,   block_5_second_break_end_time datetime)

insert into timetable values(
'8/6/2013', 1,
    '8/6/13 6:00 AM',   '8/6/13 10:00 AM',
    '8/6/13 11:00 AM',  '8/6/13 6:00 PM',
    '8/6/13 6:30 PM',   '8/6/13 8:00 PM'
    '8/6/13 7:00 AM',   '8/6/13 8:00 AM',
    '8/6/13 5:00 PM',   '8/6/13 5:30 PM')

另一张表:

create table jobtable(tech_id   int,Job_starting_time datetime, Job_end_time datetime)

insert into jobtable values(1,  '8/6/13 7:00 AM','8/6/13 8:00 AM')

现在需要为employee.pl分享一天中的空闲时间,我们如何才能实现。

1 个答案:

答案 0 :(得分:0)

您的数据结构应规范化为块/作业列表

TechID   StartDateTime   EndDateTime
1        2013-6-8 06:00  2013-6-8 10:00
1        2013-6-8 11:00  2013-6-8 18:00
...

然后更容易找到与现有数据不相交的时间块,并且您不会将您的技术人员限制在每天的有限数量的时间段内。