Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2011
    Posts
    1

    Unanswered: Running total query

    I need help with running total query in oracle.

    When I execute the query below:

    select count(order_id), platform_id from itc_com_order_header
    group by platform_id

    Output:
    COUNT(ORDER_ID) PLATFORM_ID
    8 CC
    5 GC
    4520 GT

    I want something like

    COUNT(ORDER_ID) PLATFORM_ID Total
    8 CC 8
    5 GC 13
    4520 GT 4533

    Thanks in advance.

  2. #2
    Join Date
    Mar 2007
    Posts
    623
    Firstly, you have to define the order in which is that SUM calculated. As the query you posted does not contain ORDER BY clause, it is now not defined, so e.g. correct result may also be this:
    Code:
    COUNT(ORDER_ID) PLATFORM_ID Total
    4520 GT 4520
    5 GC 4525
    8 CC 4533
    After you determine the order of rows, use analytical SUM function, something like this:
    Code:
    select cnt, platform_id, sum(cnt) over (order by <expression defining order>) total
    from ( select count(order_id) cnt, platform_id
           from itc_com_order_header
           group by platform_id )
    order by <expression defining order>;
    For more details, please consult SQL Reference book. It is available with other Oracle documentation e.g. online on http://tahiti.oracle.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •