繁体   English   中英

Oracle SQL 查询缺失月份

[英]Oracle SQL query missing month

你能帮我解决下面的问题吗?

我需要在决赛桌上获得所有 MARKS(总共 33 个),但查询仅显示 23 个,因为任何特定月份/年份(例如:7/2020)都没有航班。

我将表 TT1(有一列包含最后一次飞行日期日志)与表 AF 连接在一起(只有在记录了航班时才填充飞行日期列)。

请在我迄今为止构建的查询下方找到:

SELECT DISTINCT
    TT1.MO_LAST_FLIGHT,
    TT1.YR_LAST_FLIGHT,
    extract(month from AF.FLIGHT_DATE) MO_FLIGHT_DATE,
    extract(year from AF.FLIGHT_DATE) YR_FLIGHT_DATE,
    
    CASE WHEN extract(month from AF.FLIGHT_DATE) >= TT1.MO_LAST_FLIGHT THEN TT1.MO_LAST_FLIGHT ELSE extract(month from AF.FLIGHT_DATE) END MO,
    CASE WHEN extract(year from AF.FLIGHT_DATE) >= TT1.YR_LAST_FLIGHT THEN TT1.YR_LAST_FLIGHT ELSE extract(year from AF.FLIGHT_DATE) END YR,
        
    TT1.FLEET,
    TT1.MARKS,
    TT1.AIRCRAFT_SN,
    ROUND(Max(( ( AF.total_ac_flight_hours ) * 60 + ( AF.total_ac_flight_minutes ) ) / 60),2) TOTAL_FH,
    Max(AF.total_ac_cycles) TOTAL_FC,
    ROUND((( ( Sum(AF.flight_hours) * 60 ) + Sum(AF.flight_minutes) ) / 60), 2) MONTH_FH,
    ( Sum(AF.cycles) ) MONTH_CY,
    COUNT(DISTINCT AF.flight_date) DAYS_IN_SERVICE
        
FROM
    (SELECT
        extract(month from AM.last_date_flight_log_applied ) MO_LAST_FLIGHT,
        extract(year from AM.last_date_flight_log_applied) YR_LAST_FLIGHT,
        AM.ac_type || '-' || AM.ac_series FLEET,
        AM.ac MARKS,
        AM.ac_sn AIRCRAFT_SN
    
    FROM
        ODB.ac_master AM
    
    WHERE
        AM.status = 'ACTIVE'
    
    GROUP BY 
        extract(month from AM.last_date_flight_log_applied),
        extract(year from AM.last_date_flight_log_applied),
        AM.ac_type || '-' || AM.ac_series,
        AM.ac,
        AM.ac_sn

    ORDER BY
        extract(month from AM.last_date_flight_log_applied) DESC,
        extract(year from AM.last_date_flight_log_applied) DESC,
        AM.ac ASC
    )TT1
    FULL OUTER JOIN
    odb.ac_actual_flights AF
    ON TT1.MARKS = AF.ac

WHERE
    TT1.FLEET NOT LIKE '%SIM%' AND
    TT1.FLEET = 'ATR72-600' AND
    CASE WHEN extract(month from AF.FLIGHT_DATE) >= TT1.MO_LAST_FLIGHT THEN TT1.MO_LAST_FLIGHT ELSE extract(month from AF.FLIGHT_DATE) END = '2' AND
    CASE WHEN extract(year from AF.FLIGHT_DATE) >= TT1.YR_LAST_FLIGHT THEN TT1.YR_LAST_FLIGHT ELSE extract(year from AF.FLIGHT_DATE) END = '2020'
    
    
GROUP BY 
    TT1.MO_LAST_FLIGHT,
    TT1.YR_LAST_FLIGHT,
    extract(month from AF.FLIGHT_DATE),
    extract(year from AF.FLIGHT_DATE),
    CASE WHEN extract(month from AF.FLIGHT_DATE) >= TT1.MO_LAST_FLIGHT THEN TT1.MO_LAST_FLIGHT ELSE extract(month from AF.FLIGHT_DATE) END,
    CASE WHEN extract(year from AF.FLIGHT_DATE) >= TT1.YR_LAST_FLIGHT THEN TT1.YR_LAST_FLIGHT ELSE extract(year from AF.FLIGHT_DATE) END,
    TT1.FLEET,
    TT1.MARKS,
    TT1.AIRCRAFT_SN

ORDER BY
    TT1.MO_LAST_FLIGHT DESC,
    TT1.YR_LAST_FLIGHT DESC,
    TT1.MARKS ASC

由于该问题没有包含具体的测试数据和实际/预期的查询结果,请看下面的演示,这可能会有所帮助。 假设我们有 2 张表:一张用于“预定”航班,一张用于“实际”航班。

create table TT (  -- "scheduled" flights
  scheduled date
, aircraft varchar2( 16 )
);

create table AF (  -- actual
  flightdate date
, aircraft varchar2( 16 )
);

-- actual flights
-- aircraft 1: every month
-- aircraft 2: months 2 and 3 only
-- aircraft 3: month 3 only
-- NO FLIGHTS in month 4
insert into AF ( flightdate, aircraft ) values ( date '2020-01-17', 'aircraft_1' ) ;
insert into AF ( flightdate, aircraft ) values ( date '2020-02-15', 'aircraft_1' ) ;
insert into AF ( flightdate, aircraft ) values ( date '2020-03-16', 'aircraft_1' ) ;
insert into AF ( flightdate, aircraft ) values ( date '2020-02-17', 'aircraft_2' ) ;
insert into AF ( flightdate, aircraft ) values ( date '2020-03-15', 'aircraft_2' ) ;
insert into AF ( flightdate, aircraft ) values ( date '2020-03-16', 'aircraft_3' ) ;

-- schedule for months 1-4:
-- every aircraft is scheduled to do one flight per month
insert into TT ( scheduled, aircraft ) values ( date '2020-01-10', 'aircraft_1' );
insert into TT ( scheduled, aircraft ) values ( date '2020-02-25', 'aircraft_2' );
insert into TT ( scheduled, aircraft ) values ( date '2020-03-07', 'aircraft_3' );
insert into TT ( scheduled, aircraft ) values ( date '2020-03-12', 'aircraft_1' );
insert into TT ( scheduled, aircraft ) values ( date '2020-01-25', 'aircraft_2' );
insert into TT ( scheduled, aircraft ) values ( date '2020-02-07', 'aircraft_3' );
insert into TT ( scheduled, aircraft ) values ( date '2020-02-12', 'aircraft_1' );
insert into TT ( scheduled, aircraft ) values ( date '2020-03-25', 'aircraft_2' );
insert into TT ( scheduled, aircraft ) values ( date '2020-01-07', 'aircraft_3' );
insert into TT ( scheduled, aircraft ) values ( date '2020-04-12', 'aircraft_1' );
insert into TT ( scheduled, aircraft ) values ( date '2020-04-25', 'aircraft_2' );
insert into TT ( scheduled, aircraft ) values ( date '2020-04-07', 'aircraft_3' );

询问

如果您编写一个包含 LEFT JOIN 的查询,并在 ON 子句中包含年/月(正如您在问题中提到的“任何特定月份的航班”),您可能会更接近解决方案,例如

select 
  to_char( TT.scheduled, 'YYYY-MM' ) tt_ym
, TT.aircraft tt_aircraft
, TT.scheduled
, AF.flightdate
, case when AF.flightdate is null then 'flight cancelled' end comment_
from TT left join AF 
  on to_char( AF.flightdate, 'YYYY-MM' ) = to_char( TT.scheduled, 'YYYY-MM' ) 
    and TT.aircraft = AF.aircraft
order by tt_ym, tt_aircraft
;

     TT_YM    TT_AIRCRAFT    SCHEDULED    FLIGHTDATE            COMMENT_ 
__________ ______________ ____________ _____________ ___________________ 
2020-01    aircraft_1     10-JAN-20    17-JAN-20                         
2020-01    aircraft_2     25-JAN-20                  flight cancelled    
2020-01    aircraft_3     07-JAN-20                  flight cancelled    
2020-02    aircraft_1     12-FEB-20    15-FEB-20                         
2020-02    aircraft_2     25-FEB-20    17-FEB-20                         
2020-02    aircraft_3     07-FEB-20                  flight cancelled    
2020-03    aircraft_1     12-MAR-20    16-MAR-20                         
2020-03    aircraft_2     25-MAR-20    15-MAR-20                         
2020-03    aircraft_3     07-MAR-20    16-MAR-20                         
2020-04    aircraft_1     12-APR-20                  flight cancelled    
2020-04    aircraft_2     25-APR-20                  flight cancelled    
2020-04    aircraft_3     07-APR-20                  flight cancelled  

DBfiddle 在这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM