views:

284

answers:

2

First off, I'm a total Oracle noob although I'm very familiar with SQL. I have a single cost column. I need to calculate the total cost, the percentage of the total cost, and then a running sum of the percentages. I'm having trouble with the running sum of percentages because the only way I can think to do this uses nested SUM functions, which isn't allowed.

Here's what works:

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

Here's what I'm trying to do that doesn't work:

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

Am I just going about it wrong, or is what I'm trying to do just not possible? By the way I'm using Oracle 10g. Thanks in advance for any help.

+1  A: 
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
Quassnoi
Thanks, I obviously wasn't thinking...
Dave
+4  A: 

You don't need the order by inside that inline view, especially since the outer select is doing an order by the order way around. Also, cost / SUM(cost) OVER () equals RATIO_TO_REPORT(cost) OVER ().

An example:

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.

Your initial query:

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's query contains a typo:

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

And if I correct that typo. It gives the right results, but wrongly sorted (I guess):

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.

I think this is the one you are looking for:

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.

Regards, Rob.

Rob van Wijk
@Rob: thanks for noticing the typo and issue with ORDER BY. Also, nice point with RATIO_TO_REPORT. +1
Quassnoi