2016-10-14 1 views
0

다른 테이블에 예약 된 시간 슬롯이있는 일정 일정 테이블에서 사용 가능한 모든 시간 슬롯을 출력하는보기를 만들려고합니다.예약 된 시간 프레임에서 사용 가능한 시간 슬롯을 추출하는 중

감안할 때 테이블 :

이 ScheduledTimes에서의 값이 모두 검증
ID Date   StartTime EndTime 
56 18-OCT-16 10.00.00  18.00.00 
62 21-OCT-16 11.00.00  13.00.00 
62 21-OCT-16 14.30.00  16.00.00 
62 21-OCT-16 17.00.00  17.20.00 
62 21-OCT-16 18.00.00  20.30.00 
72 27-OCT-16 11.00.00  17.00.00 
72 28-OCT-16 08.40.00  09.41.00 
72 28-OCT-16 11.00.00  12.40.00 

, 확인이 시간 프레임 안에 수 :

Table Calendar 

ID Date   StartTime EndTime 
56 18-OCT-16 10.00.00  18.00.00 
62 21-OCT-16 11.00.00  20.30.00 
72 27-OCT-16 09.30.00  17.00.00 
72 28-OCT-16 08.40.00  18.00.00 

Table ScheduledTimes 

ID Date   StartTime EndTime 
62 21-OCT-16 13.00.00  14.30.00 
62 21-OCT-16 16.00.00  17.00.00 
62 21-OCT-16 17.20.00  18.00.00 
72 27-OCT-16 09.30.00  10.00.00 
72 27-OCT-16 10.00.00  11.00.00 
72 28-OCT-16 09.41.00  11.00.00 
72 28-OCT-16 12.40.00  18.00.00 

내가 방법을 찾고 있어요 이것을 달성하기 위해 달력 시간과 서로 충돌하지 않습니다.

+0

무엇 날짜, 상영 시간 및 종료 시각의 데이터 타입? 그들은 모두 문자열로 저장되어 있습니까? 또한 ID 번호가 의미가 있다고 가정합니다 (예 : 객실 번호, 장소 등). – mathguy

답변

2

가정 사용자의 입력 열은 모든 문자열 :

with 
    calendar (id, dt, starttime, endtime) as (
     select 56, '18-OCT-16', '10.00.00', '18.00.00' from dual union all 
     select 62, '21-OCT-16', '11.00.00', '20.30.00' from dual union all 
     select 72, '27-OCT-16', '09.30.00', '17.00.00' from dual union all 
     select 72, '28-OCT-16', '08.40.00', '18.00.00' from dual 
    ), 
    scheduledtimes (id, dt, starttime, endtime) as (
     select 62, '21-OCT-16', '13.00.00', '14.30.00' from dual union all 
     select 62, '21-OCT-16', '16.00.00', '17.00.00' from dual union all 
     select 62, '21-OCT-16', '17.20.00', '18.00.00' from dual union all 
     select 72, '27-OCT-16', '09.30.00', '10.00.00' from dual union all 
     select 72, '27-OCT-16', '10.00.00', '11.00.00' from dual union all 
     select 72, '28-OCT-16', '09.41.00', '11.00.00' from dual union all 
     select 72, '28-OCT-16', '12.40.00', '18.00.00' from dual 
    ), 
    u (id, dt, startdatetime, enddatetime) as (
     select id, dt, to_date(dt || starttime, 'dd-MON-yyhh24.mi.ss'), 
         to_date(dt || endtime , 'dd-MON-yyhh24.mi.ss') 
     from scheduledtimes 
     union all 
     select id, dt, null, to_date(dt || starttime, 'dd-MON-yyhh24.mi.ss') 
     from calendar 
     union all 
     select id, dt, to_date(dt || endtime, 'dd-MON-yyhh24.mi.ss'), null 
     from calendar 
    ), 
    prep (id, dt, starttime, endtime) as (
     select id, dt, to_char(enddatetime, 'hh24:mi:ss') as starttime, 
       to_char(lead(startdatetime) over (partition by id, dt 
          order by enddatetime), 'hh24:mi:ss') as endtime 
     from u 
    ) 
select id, dt, starttime, endtime 
from prep 
where starttime < endtime 
order by id, dt, endtime; 
관련 문제