我有employees表,我试图只查询时间戳范围内可用的雇员。

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之间为图思工作,我应该得到emp3和emp4,因为他们在指定的时间范围内不忙。
谢谢您。

最佳答案

我想你想要:

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
                 );

08-26 13:34