2012-11-08 80 views
1

我試圖從一組數據中獲得一個滾動的12個月總計,該數據提供給定的月份和年份的分鐘數。在很多情況下,我不會有一個月的記錄。是否有一種方法可以顯示丟失的月份(值爲零),然後將其捲起或不顯示,但讓查詢確認這是12個月範圍的一部分。下面的查詢是我試圖使用的,但它只是抓住了以前的記錄,並沒有考慮到失蹤的月份。下面的查詢似乎沒有與一些總數有一點關係。任何援助將不勝感激。如何獲得一個月範圍的滾動年度總計

Select year 
     , month 
     , SUM(NVL(minutes, 0)) OVER (order by year,month rows between 11 preceding and current row) as total_minutes 
    from 
    (
    Select * 
    from test_table 
    Order by year, month 
    ) 
    Order by year desc , month desc 

這裏是創建測試表和數據的sql。

drop table test_table; 
    create table test_table (
     month number(2), 
     year  number(4), 
     minutes number(4,2) 
    ); 

    insert into test_table values (1, 2012, 3); 
    insert into test_table values (2, 2012, 3); 
    insert into test_table values (3, 2012, 3); 
    insert into test_table values (4, 2012, 4); 
    insert into test_table values (5, 2012, 5); 
    insert into test_table values (7, 2012, 5); 
    insert into test_table values (8, 2012, 5); 
    insert into test_table values (9, 2012, 5); 
    insert into test_table values (10, 2012, 4); 
    insert into test_table values (11, 2012, 3); 
    insert into test_table values (12, 2012, 3); 
    insert into test_table values (1, 2011, 3); 
    insert into test_table values (2, 2011, 3); 
    insert into test_table values (5, 2011, 5); 
    insert into test_table values (6, 2011, 5); 
    insert into test_table values (7, 2011, 5); 
    insert into test_table values (8, 2011, 5); 
    insert into test_table values (9, 2011, 5); 
    insert into test_table values (10, 2011, 4); 
    insert into test_table values (11, 2011, 3); 
    insert into test_table values (12, 2011, 3); 
    insert into test_table values (1, 2010, 3); 
    insert into test_table values (2, 2010, 3); 
    insert into test_table values (3, 2010, 3); 
    insert into test_table values (4, 2010, 4); 
    insert into test_table values (5, 2010, 4); 
    insert into test_table values (6, 2010, 5); 
    insert into test_table values (7, 2010, 5); 
    insert into test_table values (10, 2010, 4); 
    insert into test_table values (11, 2010, 4); 
    insert into test_table values (12, 2010, 3); 

    COMMIT; 

回答

0

我確實使用了外連接,但沒有給出我需要的滾動12個月範圍。我不得不使用窗口子句來讓範圍和數字出來。

with m_num as(
     select level as mn 
     from dual 
    connect by level <= 12 
    ) 
    select year 
     , mn 
     , minutes 
     , sum(minutes) over(order by year,mn rows between 11 preceding and current row) rt 
    from (select tt.Year 
       , mn.mn 
       , nvl(tt.minutes, 0) minutes 
      from test_table tt 
      partition by (tt.year) 
      right outer join m_num mn 
       on (tt.month = mn.mn) 
     ) 
    order by year desc, mn desc; 
0

爲了填補空白個月partition outer join(Oracle 10g的廣告更高)使用下面的查詢,如果你真的需要看到那些零,否則你可以保留原來的查詢重寫SUM功能SUM(NVL(minutes, 0)) OVER (partition by year order by year,month) as total_minutes

SQL> with m_num as(
    2 select level as mn 
    3  from dual 
    4 connect by level <= 12 
    5 ) 
    6 select year 
    7  , mn 
    8  , minutes 
    9  , sum(minutes) over(partition by year order by year, mn) rt 
10 from (select tt.Year 
11    , mn.mn 
12    , nvl(tt.minutes, 0) minutes 
13   from test_table tt 
14   partition by (tt.year) 
15   right outer join m_num mn 
16    on (tt.month = mn.mn) 
17  ) 
18 order by year desc, mn desc 
19 ; 


YEAR1   MN MINUTES   RT 
----- ---------- ---------- ---------- 
2012   12   3   43 
2012   11   3   40 
2012   10   4   37 
2012   9   5   33 
2012   8   5   28 
2012   7   5   23 
2012   6   0   18 
2012   5   5   18 
2012   4   4   13 
2012   3   3   9 
2012   2   3   6 
2012   1   3   3 
2011   12   3   41 
2011   11   3   38 
2011   10   4   35 
2011   9   5   31 
2011   8   5   26 
2011   7   5   21 
2011   6   5   16 
2011   5   5   11 
2011   4   0   6 
2011   3   0   6 
2011   2   3   6 
2011   1   3   3 
+0

謝謝你的幫助。我不確定如何回覆解決方案,因爲我不得不爲自己的工作做出一些改變。無論如何,我真的很感激它。 –

相關問題