[英]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.