2015-10-20 35 views
3

實例數據(完整的表有行更多的列數以百萬計):的Oracle SQL GROUP BY CUBE具有鮮明的ID

invoice_number |year   |department  |euros 
------------------------------------------------------------- 
1234   |2010   |1    | 200 
1234   |2011   |1    | 200 
1234   |2011   |2    | 200   
4567   |2010   |1    | 450 
4567   |2010   |2    | 450 
4567   |2010   |3    | 450 

我的目的:

我要總結的歐元每年和每個可能的組合中的每個部門。

結果應該如何看待:

year    |department   |euros 
-------------------------------------------- 
2010    |1     |650 
2010    |2     |450 
2010    |3     |450 
2010    |(null)    |650 
2011    |1     |200 
2011    |2     |200 
(null)   |1     |650 
(null)   |2     |650 
(null)   |3     |450 
(null)   |(null)    |650 

我的查詢:

select  year 
,   department 
,   sum(euros) 
from  table1 
group by cube (
        year 
      ,  department 
        ) 

問題:

一個發票號碼可以發生在幾類。例如,一張發票可以有2010年和2011年的項目。當我想每年顯示數據時,這沒有問題。但是,當我想要總計所有年份的歐元總計兩次,每年一次。我想要「按立方體分組」的功能,但我只想彙總不同的發票號碼。

問題表:

year    |department   |euros 
-------------------------------------------- 
2010    |1     |650 
2010    |2     |450 
2010    |3     |450 
2010    |(null)    |1550 
2011    |1     |200 
2011    |2     |200 
(null)   |1     |850 
(null)   |2     |650 
(null)   |3     |450 
(null)   |(null)    |1950 

是否有可能做我想做什麼?到目前爲止,我的搜索沒有結果。我創建了一個SQL Fiddle,我希望它的工作原理

回答

1

[刪除以前的「解決方案」]

新嘗試:這裏是一個非常醜陋的解決方案,但它似乎工作,即使兩張發票有相同數量。有兩個表訪問,你應該檢查性能是否可以接受。

SQL> with table1_cubed as 
    2 (select year 
    3   , department 
    4   , grouping_id(year,department) gid 
    5  from table1 
    6  group by cube(year,department) 
    7 ) 
    8 , join_distinct_invoices as 
    9 (select distinct x.* 
10   , r.invoice_number 
11   , r.euros 
12  from table1_cubed x 
13   inner join table1 r on (nvl(x.year,r.year) = r.year and nvl(x.department,r.department) = r.department) 
14 ) 
15 select year 
16  , department 
17  , sum(euros) 
18 from join_distinct_invoices 
19 group by year 
20  , department 
21  , gid 
22 order by year 
23  , department 
24/

     YEAR DEPARTMENT   SUM(EUROS) 
---------- -------------------- ---------- 
     2010 1       650 
     2010 2       450 
     2010 3       450 
     2010        650 
     2011 1       200 
     2011 2       200 
     2011        200 
      1       650 
      2       650 
      3       450 
             650 

11 rows selected. 
+0

目前正在測試您的新解決方案以提升性能。 – Elisa

+1

當我添加更多數據列時性能下降(顯然)。當我添加所有列時,多維數據集組生成287.912行。對於一個較小的組,我'只'有4.488行。第二個有一個可以接受的表現,第一個沒有。但解決方案似乎工作。 – Elisa

0
select year 
     ,department 
     ,case when GROUPING_id(year,department) in (3) then sum(dist_euro) else sum(euros) end sums 
     ,decode(GROUPING_id(year,department),0,'NO GROUP',1,'DEPARTMENT IS NULL',2,'YEAR IS NULL',3,'TOTAL OVER ALL YEARS') info 
     from (
select  year 
      ,   department 
      ,   euros 
      ,case when row_number() over(partition by year order by year) = 1 then euros else 0 end dist_euro 
from table1) 
group by cube (
        year 
      ,  department 
        ) 
     order by GROUPING_id(year,department) 
+0

這將工作,如果我只想'年'欄的總和。但是,我希望這可能爲每列(所以在這個例子中也爲部門)和我的真實數據集中的其他7列。通過您的查詢,部門中(null)的總和仍然不正確。 – Elisa