分组为期3个月

时间:2013-09-21 16:46:57

标签: sql date count oracle11g group-by

我在Oracle中创建了一个select,它返回机组人员每月过夜的次数(如果有的话):

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

现在,3个月内限制为18个晚上停止。因此,我想在接下来的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;

1 个答案:

答案 0 :(得分: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和order by by year * 12 + month一起确保采用三个月的窗口,而不仅仅是连续三行。如果您的基本查询中缺少月份,则这是相关的。

玩得开心。