2017-02-19 51 views
0

我想根據最小開始日期顯示按12個月期間分組的數據。如果FTE在12個月期間發生了變化,則會列出相應的開始日期和結束日期。測試數據被提供爲的[Data grouped by date in oracle sqlOracle查詢 - 顯示每年分組的數據

一部分。例如開始日期19/05/2008。因此,一年的開始時間是19/05/2008,年底是18/05/2009。如果FTE在2009年5月19日至2010年5月18日期間發生變化,那麼在第二週年期間,結果集將有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 

回答

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.