2013-09-21 89 views
5

我在甲骨文這對機組人員返回過夜每月(如果有的話)的數量創造了一個選擇:集團通過3個月的時間

CRE_ALPHA CRE_NAME MONTH YEAR NIGHT_STOPS 
---------- --------- ------ ----- ------------ 
AAC  Adinda 6  2013 8 
AAC  Adinda 7  2013 9 
AAC  Adinda 8  2013 2 
AAC  Adinda 9  2013 7 
AAC  Adinda 10  2013 4 
CCU  Cristiano 6  2013 5 
CCU  Cristiano 7  2013 6 
CCU  Cristiano 8  2013 3 
CCU  Cristiano 9  2013 11 
CVA  Carine 7  2013 9 
CVA  Carine 9  2013 10 
CVA  Carine 10  2013 10 

現在,有18夜的限位擋塊上以3個月爲基礎。所以我想在接下來的3個月裏進行18次以上的晚上聚會。結果應該是這樣的:

CRE_ALPHA CRE_NAME TIMESPAN  NIGHT_STOPS 
---------- --------- --------------- ------------ 
AAC  Adinda 6/2013-8/2013 19 
AAC  Adinda 7/2013-9/2013 18 
CCU  Cristiano 7/2013-9/2013 20 
CVA  Carine 7/2013-9/2013 19 
CVA  Carine 8/2013-10/2013 20 

需要注意的是,如果有晚上零點停止了一個月,沒有行,但我想了3個月,包括一個以0

結果

任何人都可以幫忙嗎?

如果它可以幫助,全面下面選擇:


ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MM-YYYY hh24:mi:ss'; 
SELECT cre_id, cre_alpha, cre_first_name, cre_last_name, Maand, Jaar, count(*) "Night stops" 
FROM 
    (SELECT cre_id, cre_alpha, cre_first_name, cre_last_name, pos_crb_iata_code, dst, det, dsa, (dst - Prev_end_time) * 1440 stop_over, EXTRACT(MONTH FROM dst) Maand, EXTRACT(YEAR FROM dst) Jaar 
    FROM 
    (SELECT cre_id, cre_alpha, cre_first_name, cre_last_name, pos_crb_iata_code, dst, dsa, det, dea, LAG(det) OVER (ORDER BY cre_alpha, dst) Prev_end_time 
    FROM 
     (SELECT cre_id, cre_alpha, cre_first_name, cre_last_name, pos_crb_iata_code, 
     COALESCE(flt_mvt_db, flt_com_dep_blk, pog_std, gco_start, oth_std, rsv_std) as dst, 
     COALESCE(flt_mvt_ab, flt_com_arr_blk, pog_sta, gco_end, oth_sta, rsv_sta) as det, 
     COALESCE(flt_apt_iata_code_dep, pog_apt_iata_code_from, gco_apt_iata_code, rsv_apt_iata_code) as dsa, 
     COALESCE(flt_apt_iata_code_arr, pog_apt_iata_code_to, gco_apt_iata_code, rsv_apt_iata_code) as dea 
     FROM 
     (SELECT DISTINCT cre_id, cre_alpha, cre_first_name, cre_last_name, pos_crb_iata_code 
     FROM master.crews, master.assignments, master.positions 
     WHERE asg_pos_id = pos_id AND asg_cre_id = cre_id AND asg_d_type <> 'LEA' 
     AND asg_start_time BETWEEN '01-JUN-2013' AND '01-NOV-2013' 
     ORDER BY cre_alpha) tab1, master.assignments 
     FULL OUTER JOIN master.flights ON master.assignments.asg_flt_id = master.flights.flt_id 
     FULL OUTER JOIN master.positionings ON master.assignments.asg_pog_id = master.positionings.pog_id 
     FULL OUTER JOIN master.ground_courses ON master.assignments.asg_gco_id = master.ground_courses.gco_id 
     FULL OUTER JOIN master.other_duties ON master.assignments.asg_oth_id = master.other_duties.oth_id 
     FULL OUTER JOIN master.reserves ON master.assignments.asg_rsv_id = master.reserves.rsv_id 
     WHERE asg_d_type <> 'LEA' AND asg_d_type <> 'STP' AND asg_cre_id = tab1.cre_id 
     AND asg_start_time BETWEEN '01-JUN-2013' AND '02-NOV-2013' AND asg_actif = 'Y' 
     ORDER BY cre_alpha, asg_start_time) 
    ) 
    WHERE pos_crb_iata_code <> dsa 
    AND EXTRACT(DAY FROM dst) - EXTRACT(DAY FROM Prev_end_time) >= 1) 
WHERE stop_over > 240 
GROUP BY cre_id, cre_alpha, cre_first_name, cre_last_name, Maand, Jaar 
ORDER BY cre_alpha; 

回答

3

您可以使用分析功能來實現你想要什麼。您目前的查詢之上建立,它是這樣的:

select * 
from (
    select cre_alpha, cre_name, 
    month month_end, year year_end, 
    sum(night_stops) over (
     partition by cre_alpha, cre_name 
     order by year * 12 + month 
     range between 2 preceding and current row 
    ) as night_stops 
    from (
     ... your current query ... 
    ) t 
) m 
where night_stops >= 18 

注:

  • 查詢返回的3個月期間結束時(年/月)。您必須將其擴展以打印期間的開始。
  • 我已經使用條件>= 18來匹配您的輸出,即使文字說明它是> 18
  • 窗口條款range between 2 preceding and current row連同按順序排列的條款year * 12 + month確保獲得三個月的窗口,而不是連續三行。如果您在基本查詢中缺少月份,這是相關的。

玩得開心。

+0

「year * 12 + month」的排序與「month」的排序有什麼不同? '確保一個三個月的窗口被拍攝,而不僅僅是連續三行'怎麼來? –

+0

如果所有行都來自同一年,那麼您只能使用「月」而不是「年* 12 +月」。但是如果他們來自不同的年份,那麼重要的是2012年12月和2013年1月分配有連續數字,因此您可以正確應用窗口條款。啊哈! – Codo

+0

啊!然後它是有道理的。 –