Oracle查询 - 显示每年分组的数据

时间:2017-02-19 08:16:41

标签: sql oracle date

我正在尝试根据最小开始日期显示按12个月分组的数据。如果在12个月期间更改了FTE,则会列出相应的开始和结束日期。测试数据作为[Data grouped by date in oracle sql

的一部分提供

例如,开始日期是19/05/2008。因此,一年的开始时间是19/05/2008,年底是18/05/2009。如果FTE在19/05/2009和18/05/2010之间发生变化,那么结果集将在2周年期间有2行。

YearEmployeeJob Start date  end date    FTE Year_start  Year_end
1   1111    1   19/05/2008  18/05/2009  100 19/05/2008  18/05/2009
2   1111    1   19/05/2009  18/05/2010  100 19/05/2009  18/05/2010
3   1111    1   19/05/2010  18/05/2011  100 19/05/2010  18/05/2011
4   1111    1   19/05/2011  17/05/2012  100 19/05/2011  17/05/2012
5   1111    1   19/05/2012  18/05/2013  100 19/05/2012  18/05/2013
6   1111    1   19/05/2013  27/11/2013  100 19/05/2013  18/05/2014
6   1111    1   28/11/2013  18/05/2014  60  19/05/2013  18/05/2014
7   1111    1   19/05/2014  18/05/2015  60  19/05/2014  18/05/2015
8   1111    1   19/05/2015  24/08/2015  60  19/05/2015  17/05/2016
8   1111    1   25/08/2015  18/05/2016  68.571  19/05/2015  17/05/2016

1 个答案:

答案 0 :(得分:0)

请参阅原始帖子Data grouped by date in oracle sql

中提供的解决方案

在下面的解决方案中,我在标记为y的列中向该查询添加了一点信息,以显示在那里创建的每个间隔的年数(四舍五入到最接近的整数)。我在附加的包络(外部)查询中使用此位来生成"年度"新要求中的间隔。标准的方法是使用传统的分层查询,如下所示。

select   level as yr, employee, job,
         add_months(start_date, 12 * (level - 1)) as start_date,
         case when level < y then add_months(start_date, 12 * level) - 1
              else end_date end    as end_date,
         FTE
from (
  select   employee, job, min(start_date) as start_date, max(end_date) as end_date, FTE,
           ceil(months_between(max(end_date), min(start_date))/12) as y
  from     ( select employee, job, start_date, end_date, FTE,
                 row_number() over (partition by employee, job      order by start_date) -
                 row_number() over (partition by employee, job, FTE order by start_date) 
                                                                                 as grp
             from t
           )
  group by employee, job, FTE, grp
     )
connect by level <= y
    and prior employee   = employee
    and prior start_date = start_date
    and prior sys_guid() is not null
order by employee, start_date       -- if needed
;

<强>输出

 YR EMPLOYEE   JOB   START_DATE  END_DATE           FTE
--- ---------- ----- ----------- ----------- ----------
  1 1111       1     19-May-2008 18-May-2009        100
  2 1111       1     19-May-2009 18-May-2010        100
  3 1111       1     19-May-2010 18-May-2011        100
  4 1111       1     19-May-2011 18-May-2012        100
  5 1111       1     19-May-2012 18-May-2013        100
  6 1111       1     19-May-2013 27-Nov-2013        100
  1 1111       1     28-Nov-2013 27-Nov-2014         60
  2 1111       1     28-Nov-2014 24-Aug-2015         60
  1 1111       1     25-Aug-2015 24-Aug-2016     68.571
  2 1111       1     25-Aug-2016 11-Sep-2016     68.571
  1 1111       1     12-Sep-2016 29-Sep-2016     72.857
  1 1111       1     30-Sep-2016 10-Nov-2016     91.429
  1 1111       1     11-Nov-2016 23-Apr-2017        100
  1 1111       1     24-Apr-2017 14-Jan-2018         60
  1 1111       1     15-Jan-2018 14-Jan-2019     71.429
  2 1111       1     15-Jan-2019 31-May-2019     71.429
  1 1111       1     01-Jun-2019 31-May-2020        100
  2 1111       1     01-Jun-2020 31-May-2021        100
  3 1111       1     01-Jun-2021 31-May-2022        100
  4 1111       1     01-Jun-2022 31-May-2023        100
  5 1111       1     01-Jun-2023 31-May-2024        100
        .............................
 29 1111       1     01-Jun-2047 31-May-2048        100
 30 1111       1     01-Jun-2048 31-May-2049        100
 31 1111       1     01-Jun-2049 31-Dec-2049        100

47 rows selected.