员工排班系统

时间:2019-03-13 01:52:25

标签: sql database postgresql

我有employees表,我想只查询时间戳范围内可用的employee。

select 
    emp_fn_name, emp_fn_name,
    TO_TIMESTAMP(e.start_time, 'HH12:MIAM,PM')::TIME,
    TO_TIMESTAMP(e.end_time, 'HH12:MIAM,PM')::TIME
from 
    employees e
inner join 
    schedule s on emp_id = e.id
where 
    TO_TIMESTAMP(s.start_time, 'HH12:MIAM,PM')::TIME
        not BETWEEN TO_TIMESTAMP('2:00PM', 'HH12:MIAM,PM')::TIME
                AND TO_TIMESTAMP('03:00PM', 'HH12:MIAM,PM')::TIME

进度表的样本数据:

start   end     days    emp_id  emp_fn
2:00    3:00PM  TuTh    38      e1
2:00    3:00PM  TuTh    154     e2
4:00    5:00AM  TuTh    154     e3
6:00    7:00AM    MW    154     e1
7:00    8:00AM    MW    154     e4

如果我正在寻找从2:00到3:00 pm进行TuTh工作的人,我应该得到emp3和emp4,因为他们在指定的时间范围内并不忙。

谢谢。

1 个答案:

答案 0 :(得分:2)

我想你想要

select emp_fn_name, emp_fn_name
from employees e 
where not exists (select 1
                  from schedule s
                  where s.emp_id = e.id and
                        s.start < '03:00PM'::time and
                        s.end > '02:00PM'::time
                 );