2009-06-03 94 views
3

首先,儘管我對SQL非常熟悉,但我是Oracle noob總。我有一個單一的成本欄。我需要計算總成本,總成本的百分比,然後計算百分比的運行總和。我在運行百分比總和時遇到問題,因爲唯一可以這樣做的方法是使用嵌套SUM函數,這是不允許的。涉及另一個計算結果的Oracle計算

這裏是什麼工作:

SELECT cost, SUM(cost) OVER() AS total, cost/SUM(cost) OVER() AS per 
FROM my_table 
ORDER BY cost DESC 

這裏就是我想要做的是不工作:

SELECT cost, SUM(cost) OVER() AS total, cost/SUM(cost) OVER() AS per, 
     SUM(cost/SUM(cost) OVER()) OVER(cost) AS per_sum 
FROM my_table 
ORDER BY cost DESC 

我只是繞了錯誤,或者是我什麼試圖做到不可能?順便說一句,我使用Oracle 10g。預先感謝您的幫助。

+0

Quassnoi和Rob van Wijk有正確的方法,使用內聯視圖。 – spencer7593 2009-06-03 20:49:25

回答

2
SELECT cost, total, per, SUM(per) OVER (ORDER BY cost) 
FROM (
     SELECT cost, SUM(cost) OVER() AS total, cost/SUM(cost) OVER() AS per 
     FROM my_table 
     ) 
ORDER BY 
     cost DESC 
+0

謝謝,我顯然不是在想...... – Dave 2009-06-03 19:08:10

6

您不需要通過內聯視圖內部的順序,尤其是因爲外部選擇按訂單方式執行順序。此外,成本/ SUM(成本)OVER()等於RATIO_TO_REPORT(成本)OVER()。

一個例子:

SQL> create table my_table(cost) 
    2 as 
    3 select 10 from dual union all 
    4 select 20 from dual union all 
    5 select 5 from dual union all 
    6 select 50 from dual union all 
    7 select 60 from dual union all 
    8 select 40 from dual union all 
    9 select 15 from dual 
10/

Table created. 

你的初始查詢:

SQL> SELECT cost, SUM(cost) OVER() AS total, cost/SUM(cost) OVER() AS per 
    2 FROM my_table 
    3 ORDER BY cost DESC 
    4/

     COST  TOTAL  PER 
---------- ---------- ---------- 
     60  200   .3 
     50  200  .25 
     40  200   .2 
     20  200   .1 
     15  200  .075 
     10  200  .05 
     5  200  .025 

7 rows selected. 

Quassnoi的查詢中包含一個錯字:

SQL> SELECT cost, total, per, SUM(running) OVER (ORDER BY cost) 
    2 FROM (
    3   SELECT cost, SUM(cost) OVER() AS total, cost/SUM(cost) OVER() AS per 
    4   FROM my_table 
    5   ORDER BY 
    6     cost DESC 
    7   ) 
    8/
SELECT cost, total, per, SUM(running) OVER (ORDER BY cost) 
           * 
ERROR at line 1: 
ORA-00904: "RUNNING": invalid identifier 

如果我糾正錯字。它提供了正確的結果,但他們的錯誤排序(我猜):

SQL> SELECT cost, total, per, SUM(per) OVER (ORDER BY cost) 
    2 FROM (
    3   SELECT cost, SUM(cost) OVER() AS total, cost/SUM(cost) OVER() AS per 
    4   FROM my_table 
    5   ORDER BY 
    6     cost DESC 
    7   ) 
    8/

     COST  TOTAL  PER SUM(PER)OVER(ORDERBYCOST) 
---------- ---------- ---------- ------------------------- 
     5  200  .025      .025 
     10  200  .05      .075 
     15  200  .075      .15 
     20  200   .1      .25 
     40  200   .2      .45 
     50  200  .25      .7 
     60  200   .3       1 

7 rows selected. 

我認爲這是你正在尋找一個:

SQL> select cost 
    2  , total 
    3  , per 
    4  , sum(per) over (order by cost desc) 
    5 from (select cost 
    6    , sum(cost) over() total 
    7    , ratio_to_report(cost) over() per 
    8    from my_table 
    9  ) 
10 order by cost desc 
11/

     COST  TOTAL  PER SUM(PER)OVER(ORDERBYCOSTDESC) 
---------- ---------- ---------- ----------------------------- 
     60  200   .3       .3 
     50  200  .25       .55 
     40  200   .2       .75 
     20  200   .1       .85 
     15  200  .075       .925 
     10  200  .05       .975 
     5  200  .025        1 

7 rows selected. 

問候, 羅布。

+0

@Rob:感謝您注意到ORDER BY的錯字和問題。此外,與RATIO_TO_REPORT的好點。 +1 – Quassnoi 2009-06-04 06:42:18