Help on Select sum

I have table
id order start end sum(a) sum(b)
1 1 0 0.1
1 2 0 0.2
1 3 0 0.3
2 1 0 1
2 2 0 2
2 3 0 3
What I need is as follows
id order start end sum(a) sum(b)
1 1 0 0.1 0 0.1
1 2 0 0.2 0.1 (0.1+0.2)
1 3 0 0.3 0.3 0.3+0.3
2 1 0 1 0 1
2 2 0 2 1 3
2 3 0 3 3 6
Thanks

I want to update table with new values
Can you help
SQL> create table mytable
  2  ( id number
  3  , ordering number
  4  , startvalue number
  5  , endvalue number
  6  , sum_a number
  7  , sum_b number
  8  )
  9  /
Tabel is aangemaakt.
SQL> insert into mytable (id,ordering,startvalue,endvalue)
  2  select 1 id, 1 ordering, 0 startvalue, 0.1 endvalue from dual union all
  3  select 1, 2, 0, 0.2 from dual union all
  4  select 1, 3, 0, 0.3 from dual union all
  5  select 2, 1, 0, 1 from dual union all
  6  select 2, 2, 0, 2 from dual union all
  7  select 2, 3, 0, 3 from dual
  8  /
6 rijen zijn aangemaakt.
SQL> merge into mytable t1
  2  using ( select rowid rid
  3               , lag(x,1,0) over (partition by id order by ordering) a
  4               , x b
  5            from ( select id
  6                        , ordering
  7                        , sum(endvalue-startvalue) over (partition by id order by ordering) x
  8                     from mytable
  9                 )
10        ) t2
11     on ( t1.rowid = t2.rid )
12   when matched then
13        update set sum_a = t2.a, sum_b = t2.b
14   when not matched then
15        insert values (null,null,null,null,null,null)
16  /
6 rijen zijn samengevoegd.
SQL> select * from mytable
  2  /
        ID   ORDERING STARTVALUE   ENDVALUE      SUM_A      SUM_B
         1          1          0         ,1          0         ,1
         1          2          0         ,2         ,1         ,3
         1          3          0         ,3         ,3         ,6
         2          1          0          1          0          1
         2          2          0          2          1          3
         2          3          0          3          3          6
6 rijen zijn geselecteerd.Regards,
Rob.

Similar Messages

  • HELP "Select SUM(x), SUM(y)" different between 8.1.6 and 8.1.7.2 ?

    The following query runs fine under 8.1.6 but returns bad result under 8.1.7.2 :
    SELECT SUM(NBANOMALIE4), SUM(NBANOMALIE2)
    FROM
    (SELECT COUNT(*) "NBANOMALIE2" FROM CPN A, ANOCPN B WHERE A.CODENS= '128' AND B.CODANO='2' AND A.NUMFOR = B.NUMFOR ) ,
    (SELECT COUNT(*) "NBANOMALIE4" FROM CPN A, ANOCPN B WHERE A.CODENS= '128' AND B.CODANO='4' AND A.NUMFOR = B.NUMFOR )
    GROUP BY NBANOMALIE2, NBANOMALIE4;
    Result given under 8.1.6 is
    SUM(NBANOMALIE4) SUM(NBANOMALIE2)
    1 0 (correct)
    Result given under 8.1.7.2 is
    0 0 (wrong)
    if query is changed to "SELECT SUM(NBANOMALIE2), SUM(NBANOMALIE4) ..."
    Result given under 8.1.7.2 is
    1 1 (wrong too !)
    actually the result given for ALL is the result of the last "SUM()" in the select ...
    Is it a bug ? if so any patch ? Is there a workaround ?
    Thanks for any help
    Charlie [email protected]

    a solution was to use decode()
    here is a workaround :
    SELECT DECODE(B.CODANO,'2',1,0) NBANOMALIE2, DECODE(B.CODANO,'4',1,0) NBANOMALIE4
    FROM CPN A, ANOCPN B
    WHERE A.CODENS= '128' AND B.CODANO IN ('2','4') AND A.NUMFOR = B.NUMFOR;
    I had it from http://www.orafaq.com/. ...
    Charlie [email protected]

  • Access the results of the following query in Java? SELECT sum(COL_X) FROM TABLE_A

    Statement stmt = conn.createStatement();
    ResultSet rset = stmt.executeQuery(
    "SELECT sum(COL_X) FROM TABLE_A");
    int = rset.getInt("sum(NUM_CUSTOMER)");This is obviously wrong. Could someone please help me a little on the syntax? I just want to be able to get the sum of the numeric values in a column without having to loop through a ResultSet and manually adding up the total. My question is how can I access the results of this query by assigning the sum to a java variable?

    Hi,
    Probably the easiest way is to use an alias for the sum column, i.e.:
    Statement stmt = conn.createStatement();
    ResultSet rset = stmt.executeQuery(
    "SELECT SUM(COL_X) MY_SUM FROM TABLE_A"
    int i = rset.getInt("MY_SUM");null

  • Select sum(distinct column ) from ....

    Hello All,
    I have a table the contains ID and size.
    An ID identifies a unique object, but it can be repeated. The following is valid:
    ID Size
    1 3
    4 5
    5 3
    1 3
    What I am trynig to do is get the sum of all sizes in the table. I tried this query:
    select sum(distinct size) from table;
    But it does give the right result since different objects could have the same size. I tried
    select sum(size) group by ID;
    But it prints to me huge number of IDs and sums the sizes of an ID together. What I want thuogh is to look at the unique rows (without duplicates) and then sum their sizes together and get the result.
    Any help would be greatly appreciated.
    Thank you
    P.S.: Sorry, I am an absolute Oracle/SQl newbie.
    Thanks for your help in advance

    An ID identifies a unique object, but it can be repeated.Fnord.
    I think your data model could withstand some refactoring but what you need to do is:
    SELECT sum(size) FROM
      (  SELECT distinct id, size FROM your_table)
    /This of course assumes that your duplication is complete and you don't have duplicate IDs with different sizes.
    You still ought to check out the concept of UNIQUE constraints though. A database ain't a database without relational integrity.
    Cheers, APC

  • SELECT SUM( field1 field2 ) INTO C1

    Is that possible with a select statement in ABAP or do I need to sum each row by itself then add them together afterwords?

    Hi Craig,
    this is not possible with a select statement in ABAP. You can use the routine mentioned by you:
    DATA: C1 TYPE i,
          C2 TYPE i,
          total type i.
    SELECT SUM( field1 ) SUM( fiedl2 ) into ( c1 c2 )from tablename.
    total = c1 + c2.
    Of course you can use the exec sql statement proposed by Andreas, but you have to select for the cllient explicitly. There are some disadvantages for native sql, too:
    1. platform dependent
    2. no syntax check for the native sql part
    3. you bypass the SAP database buffer
    For more details refer to the online help about EXEC SQL.
    Cheers,
    Morten

  • Select  sum(substr(8763,3,4)||'+'||9) from dual   how to add ?

    i want to add this substring value with 9
    select sum(substr(8763,3,4)||'+'||9) from dual
    means i need output 72

    FOR I IN C1 LOOP
    IF V_COUNT=0 THEN
    V_FILENAME:='AB'||P_DOCUMENT_NUM||'.txt';
    ELSE
    V_FILENAME:='AB'||SUBSTR(P_DOCUMENT_NUM,2,5)||+||V_COUNT||'.txt';
    END IF;
    L_FILE :=UTL_FILE.FOPEN('D:\oracle\visdb\9.2.0\plsql\temp',V_FILENAME,'W');
    UTL_FILE.PUT_LINE(L_FILE ,'E2|' || TRUNC(I.RAC_BILL_TO_CUSTOMER_NUM,0) ||l_attr||
    TRUNC(I.RAC_SHIP_TO_CUSTOMER_NUM,0) );
    V_COUNT:=V_COUNT+1;
    end loop;
    Actuelly i have write the data into different files names(i have to increase the filename for tranction) for each record of cursor
    but oracle is not allowing to add (V_FILENAME:='AB'||SUBSTR(P_DOCUMENT_NUM,2,5)||+||V_COUNT||'.txt';)
    that sum -----SUBSTR(P_DOCUMENT_NUM,2,5)||+||V_COUNT can any one help;

  • "Select sum(field)" not summing

    I have a PLSQL statement like:
    select sum(field1) sum1,
    sum (field2) sum2
    into v_sum1,
    v_sum2
    where something = somethingElse;
    How come my variable are being assigne a single row's (the first) field1 and field2 values rather than the actual sums?
    If I do a SQLPLUS version of this (without the "into" part), I get the correct sums returned in a single row.
    Thanks,

    I just tried your query and it should work if you only want a sum for a specific where clause. For example one employee's salary given the employee's id. If you want a list of salaries for every employee in a specific department, you need to put this into a cursor and loop through it.
    This will look at all the rows based on the where statement and sum them all up. The reason it works in SQL*PLUS is that an implicit cursor is used (loops through automatically).
    ex:
    DECLARE
    CURSOR C_SUM is
    select ename,
    sum(sal) sum1,
    sum(comm)sum2
    from emp
    where deptno = 30;
    BEGIN
    FOR R_SUM IN C_SUM
    LOOP
    DBMS_OUTPUT.PUT_LINE(r_sum.ename ||' '||
    r_sum.sum1 ||' '||
    r_sum.sum2);
    END LOOP;
    END;
    Make sure if you use DBMS_OUTPUT you have serveroutput on. Hope this helps. If it doesnt, post the actual code you are having problems with.

  • SQL Query Help: Average of Sums !

    Hi Folks !
    I've been tasked with wirting a program that queries Oracle DB. And I'm not used to SQL a lot. May be my question is very basic. Please bear with me.
    I've simplified the table structure for this question.
    The table Order has 4 fields:
    1. Order_id
    2. Trial_No.
    3. Response_Time.
    4. Order_Time
    Now the first two fields (Order_id, Trial_number) together make up the Primary Key (It is a composite key). My goal is to find the Average Response time for each Order ID in the past 10 minutes. Order_Time is the time at which Order was placed. An order can take more than one trail to complete the order. Order Reponse time is the sum of all the Trail response times.
    A basic query like this
    select AVG(Response_Time) from Order where Order_time < sysdate -10/1440.
    gives me the Average of the response time but here is the catch : It produces the Average no matter how many trials the Order had. An order's response time is SUM of all the Trail Response times it had.
    Consider the following data in the table
    Order_id Trial_No Resopnse_Time (ms)
    1 1 1000
    2 1 1000
    2 2 1000
    2 3 1000
    If I just use "select AVG(RESPONSE_Time), it will result in 1000ms. (4000/4).
    But in reality, Order 2 had taken 3000ms to complete (3 trials). And the Avg Order Response time should be (1000+3000)/2 = 2000ms. Can you see it?
    In other words, How do I calcluater the Avg of the sums of the rows, instead of Avg of all the rows. I've gone through some docs about 'group by' clause and 'subqueries', but don't seem to find the answer.
    Thanks very much for reading such a long post. Looking forward to some guideance.
    - K

    You'll want some analytics on this:
    SYS%xe> --select sum(sum_resp_time)/sum(rn) avg
    SYS%xe> --from (
    SYS%xe> with dataset as
      2  (select  1 Order_id, 1 Trial_No, 1000 Response_Time from dual union all
      3   select  2         , 1         , 1000 from dual union all
      4   select  2         , 2         , 1000 from dual union all
      5   select  2         , 3         , 1000 from dual
      6  )
      7  select order_id
      8  ,      response_time
      9  ,      row_number() over ( partition by order_id  order by order_id) rn
    10  ,      sum(response_time) over ( partition by order_id  order by order_id) sum_resp_time
    11  from dataset
    12  -- ) where rn = 1;
      ORDER_ID RESPONSE_TIME         RN SUM_RESP_TIME
             1          1000          1          1000
             2          1000          1          3000
             2          1000          2          3000
             2          1000          3          3000
    4 rijen zijn geselecteerd.
    Verstreken: 00:00:02.15
    SYS%xe> select sum(sum_resp_time)/sum(rn) avg
      2  from (
      3  with dataset as
      4  (select  1 Order_id, 1 Trial_No, 1000 Response_Time from dual union all
      5   select  2         , 1         , 1000 from dual union all
      6   select  2         , 2         , 1000 from dual union all
      7   select  2         , 3         , 1000 from dual
      8  )
      9  select order_id
    10  ,      response_time
    11  ,      row_number() over ( partition by order_id  order by order_id) rn
    12  ,      sum(response_time) over ( partition by order_id  order by order_id) sum_resp_time
    13  from dataset
    14  ) where rn = 1;
           AVG
          2000
    1 rij is geselecteerd.But you could do without them, as Raj showed.
    ( I guess this is one of my weirdest queries ;-) )
    Edited by: hoek on Apr 7, 2009 6:17 PM

  • I have copied many photo's from another laptop to my Mac.  The older photo's are in directories with names that help me select what I need to view. I would like to have all my imported new photo's also bee added to the directory structure I have in Finder

    I have copied many photo's from another laptop to my Mac.  The older photo's are in directories with names that help me select what I need to view. I would like to have all my imported new photo's also bee added to the directory structure I have in Finder but my new photo's are all in iPhoto.  I want to use directories for storing and iPhoto for viewing.  Is this possible or do I need to have all my photo's in iPhoto??
    Mitch

    iPhoto is not a Photo Viewer. It's a Photo Manager and designed for looking after the files while you organise the Photos. It really works much better if you let it manage those files. If you use iPhoto you never go near those files because iPhoto is your start point for anything you want to do with your Photos - the point of the pplication.
    You can run iPhoto in Referenced mode, where it does not copy the files to the Library, but I caution you that you are making life a lot more difficult for yourself by doing that.
    How to, and some comments on why you shouldn't, are in this thread
    https://discussions.apple.com/thread/3062728?tstart=0
    Regards
    TD

  • Restricting values F4 help in selection screen-Urgent

    Hi,
    can anyone pls tell how can I ristrict the values for search help in any field in selection screen.
    please tell me the way to hide some values in F4 help in selection screen so that user can not see those data for selection.
    Regards

    i think u can not hide the data.........
    create own f4 help............restrict the data and then pass it the f4 internal table....
    fro exp;;;;;
    types : begin of ty_tab,
              post_code1 like adrc-post_code1,
             end of ty_tab.
      data : it_tab type ty_tab occurs 0.
      data : lt_field type table of dfies,
             lw_field type dfies.
      select post_code1 from adrc into corresponding fields of table it_tab.
      call function 'F4IF_INT_TABLE_VALUE_REQUEST'
          exporting
            retfield        = 'POST_CODE1'
            dynpprog        = sy-repid
            dynpnr          = sy-dynnr
            dynprofield     = 'LI_LIST1'
            value_org       = 'S'
          tables
            value_tab       = it_tab
         field_tab       = lt_field
          exceptions
            parameter_error = 1
            no_values_found = 2
            others          = 3.
      if sy-subrc <> 0.
        message id sy-msgid type sy-msgty number sy-msgno
                with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      endif.
    Regards
    Anbu

  • How to created Freely Programmed F4 help in Select Options

    hi,all
       I have a probelm about how to created a Freely Programmed F4 help in Select Options,and put help value into select options field
      Thanks and Best Regards

    Hi Haung,
    You need to use FREE_HELP not the component name.
    Modify you code as below:
       LT_RANGE_TABLE_V = wd_this->m_handler_V->create_range_table( 'ZCYPHDATE' ).
      wd_this->M_HANDLER_V->add_selection_field(
          i_id                         = 'ZCYPHDATE'
          it_result                    = LT_RANGE_TABLE_V
          i_value_help_type            = if_wd_value_help_handler=>CO_PREFIX_APPLDEV
          i_value_help_id              = 'FREE_HELP'
          i_no_intervals               = abap_false ).
    Hope this helps you.
    Regards,
    Rama

  • Can i assign a collective srch help for select option in list display

    can i assign a collective srch help for select option in list display

    Hi,
    Yes ,u can assign a collective search help for select-option in list display.
    Eg:
    Define your select option like this
    SELECT-OPTIONS: s_vbeln FOR likp-vbeln MATCHCODE OBJECT vmva.
    Regards,
    Shiva.

  • How to get calender in f4 help for select options in module pool (URGENT)

    Hi All,
    how to get calender in f4 help for select options in module pool
    Please help .
    Thanx in advance,
    amruta

    Hi Amruta,
    First of all, you can not create select-options directly in module pool.
    For creating <b>select-option is dialog prog</b> follow these steps:
    1. create your selection screen model as subscreen by:
    SELECTION-SCREEN BEGIN OF SCREEN 2000 AS SUBSCREEN.
    PARAMETRS: P_MATNR LIKE MARA-MATNR.
    SELECT-OPTIONS: S_BISMAT FOR MARA-BISMAT.
    SELECTION-SCREEN END OF SCREEN 2000.
    2. create a screen ( example 100 ) in your module-pool dedicated for selection-screen. on this screen layout sketch a sub-screen name like subscree_2000.
    3. write this bit of code in PBO and PAI of the screen 100;
    CALL SUBSCREEN SUBSCREEN_2000.
    4. include this code in PBO:
    INCLUDING SY-REPID '2000'
    6. write user_command of PAI, call screen which is going to executable after selection-screen.
    5. create a transcation for this dialog module pool with screen 100.
    6. execute this transaction and you can see it's behaving like cool with select-options.
    After that in [bprocee on value-request]</b>, use F4_DATE for both from and to option field.
    Hope it will solve the problem.
    Regards
    Krishnendu

  • Need help on select statement...

    Hi,
    I need to fetch from vbfa table those records where vbeln starts with '0800'.
    my select statement given below gives a syntax error..pl help.
    SELECT * FROM vbfa WHERE vbeln(4) = '0800'.
    vbeln(4) is not accepted and i get the message ' field vbeln(4) is unknown' ...what to do?
    thks

    Use LIKE. Please see F1 on this.
    Rob
    (changed CP to LIKE)
    Edited by: Rob Burbank on Sep 15, 2008 11:18 AM

  • F4 help for select options based on parameter value

    hi all,
    I need a help to create an F4 help for select options for object id based on parameter value of object type, I mean once an object type is given the f4 help should contain object id's only of that type for each option.
    Regard's,
    Girija
    Moderator Message : Duplicate post locked. Continue with [f4 help for select options based on parameter value |f4 help for select options based on parameter value;.
    Edited by: Vinod Kumar on May 17, 2011 1:36 PM

    hi all,
    I need a help to create an F4 help for select options for object id based on parameter value of object type, I mean once an object type is given the f4 help should contain object id's only of that type for each option.
    Regard's,
    Girija
    Moderator Message : Duplicate post locked. Continue with [f4 help for select options based on parameter value |f4 help for select options based on parameter value;.
    Edited by: Vinod Kumar on May 17, 2011 1:36 PM

Maybe you are looking for

  • Can i search the trash?

    i am trying to search for a file in the trash and it seems to revert to the whole computer when i do this. when i try a search for documents folder it stays in documents folder. any ideas? is this simply a manual process looking for a file in the tra

  • Are there any programmatic means of reducing paging and increasing efficiency of insert/updates?

    We have a multi-terabyte database which daily receives tens of thousands of records to insert or update. Most online advice on reducing memory paging covers server memory options and dynamic memory management techniques. Are there any tested programm

  • Install BI Admin Cockpit

    Hi Experts, Can anyone tell me about ,"how to install BI Admin Cockpit"?????? Help me in this regard...answers will be rewarded. Thanks in advance. Shankar

  • Screen color settings changes when certain software is launched.

    Since today I noticed the screen color settings of my Macbook Pro 17" (beginning 2011 model - Running OSX 10.8.4) is changing when certain software is launched. In this case, Indesign CS6.0 gives a strange blue hue over my screen, but when I check my

  • Error Valuation & is not maintained for material & in IDOC

    Hi Experts! I'm currently having a difficult problem here. This is an idoc issue. The scenario is: An idoc is created from WEB and an error appears "Valuation & is not maintained for material &" in SAP. But when we reporcess again usign the same data