我如何根据一年的时间表创建单个航班条目?

问题描述 投票:1回答:2

我已获得了明年所有航班时间表的数据,并被要求将这些信息转换成单独的条目。

我拥有的数据看起来像这样:

Flight number ¦  Schedule start  ¦ Schedule End  ¦  Day of Week  ¦ Origin ¦ Destination ¦ Departure time
-------------------------------------------------------------------------------------------
1234                 4 Nov 2019          3 Dec 2019          123  7           
1234                 7 Nov 2019          2 Dec 2019             56             
1234                 3 Dec 2019          7 Mar 2019          1    7        

基本上,它是列出的时间段(时间表开始和结束日期)之间一周中的每一天的航班时间表。我需要将其转换为单独的条目(例如您在机场屏幕上看到的内容,但整年都获得了我的数据):

Departure date¦ Departure time¦ Flt number¦ Origin ¦ Destination


使用上面的时间表信息。我完全不知道如何开始,有人可以给我一些想法吗?

谢谢

sql oracle schedule
2个回答
0
投票

我认为您的解决方案需要基本表中缺少一些信息,但这是如何解决此问题的基本思想:

WITH "YEAR" AS (SELECT '2019' "YEAR" FROM dual)
   , "DAYS" AS (SELECT TO_DATE("YEAR"||'0101', 'YYYYMMDD') + LEVEL - 1 "DAY"
                  FROM "YEAR"
                 CONNECT BY TO_CHAR(TO_DATE("YEAR"||'0101', 'YYYYMMDD') + LEVEL - 1, 'YYYY') = "YEAR")
   , dat(flight_no, sch_start, sch_end, day_of_week) AS
     (SELECT 1234, DATE'2019-11-04', DATE'2019-12-03', '1237' FROM dual UNION ALL
     SELECT 1234, DATE'2019-11-07', DATE'2019-12-02', '56' FROM dual UNION ALL
     SELECT 1234, DATE'2019-12-03', DATE'2020-03-07', '17' FROM dual)
SELECT d."DAY"
     , dat.*
     , TO_CHAR(d."DAY", 'D')
  FROM "DAYS" d
  JOIN dat 
   ON d."DAY" BETWEEN dat.sch_start AND dat.sch_end
   AND INSTR(day_of_week, TO_CHAR(d."DAY", 'D')) > 0

查询“ DAY”会给您一年中的所有天作为DATE。然后,您可以使用计划间隔和星期几作为条件将它们加入航班数据。


-1
投票

喜欢这样

with shed as
(select 1234 fn, to_date('04.11.2019','dd.mm.yyyy') SSt, to_date('03.12.2019','dd.mm.yyyy') SEnd, '1237' DOW from dual
union all 
select 1234 , to_date('07.11.2019','dd.mm.yyyy') , to_date('03.12.2019','dd.mm.yyyy') , '56'  from dual
union all 
select 1234 , to_date('02.12.2019','dd.mm.yyyy') , to_date('07.03.2020','dd.mm.yyyy') , '17'  from dual),
d as
(select trunc(sysdate,'YEAR')-1+level dt from dual
connect by level<365)
select s.fn,s.sst,s.send,d.dt from shed s
join d on d.dt between s.sst and s.send and instr(s.dow,to_char(d.dt, 'd'))>0
© www.soinside.com 2019 - 2024. All rights reserved.