2016-10-14 38 views
0

我正在嘗試創建一個視圖,該時間將從計劃日曆表中輸出所有可用時間段,並在另一個表上具有已預訂的時間段。從預定時間範圍內提取可用時間段

鑑於表:

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 

我正在尋找一種方式來實現這一目標:

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 

在ScheduledTimes值都證實,肯定要的時間框架內日曆時間並不相互衝突。

+0

什麼是日期,開始時間和結束時間的數據類型?它們是否全部存儲爲字符串?另外,我認爲身份證是有意義的(如房間號碼,場地等)? – 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; 
相關問題