简体   繁体   中英

Oracle SQL query missing month

Could you help me with the issue below?

I need to have all MARKS (total of 33) on final table, but the query is only showing 23 due to there were no flight for any specific month / year (for ex: 7/2020).

I have got a join with table TT1 (that have a column with the last flight date log) together table AF (that have flight date column filled only if there are flights logged).

Please find below the query I have built so far:

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

As the question contains no specific test data and actual/expected query results, please have a look at the following demo, which may help. Suppose we have 2 tables: one for "scheduled" flights, one for "actual" flights.

TABLES

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

Query

If you write a query containing a LEFT JOIN, and include the year/month in the ON clause (as you had mentioned "flight for any specific month" in your question), you may get closer to the solution eg

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 here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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