Sum in 2 tables?

How to get sum of 2 tables on the third table on one sheet?

Regarding adding a screen shot to your post:
Command-Shift-4, Click and Drag over the area you want to image.
The graphic file will appear on your Desktop.
Upload the file to a photo-sharing internet site. You will need to create an account on one if you haven't already done that.
Select the doc on the sharing site and copy the link info.
Paste the link into your post.
Regards,
Jerry

Similar Messages

  • USING SUM IN INTERNAL TABLE

    plz give me a simple example for using SUM in internal table and do some calculations in the same internal table.

    HI
    CHECK WITH THIS
    Syntax
    SUM.
    Effect
    The statement SUM can only be specified within a loop starting with LOOP, and is only considered within a AT- ENDAT control structure. Prerequisites for using the statement SUM include using the addition INTO in the LOOP statement, and that the specified work area wa is compatible with the row type of the internal table. In addition, SUM cannot be used when the row type of the internal table itab contains components that are tables.
    The statement SUM calculates the component total with the numeric data type (i, p, f) of all rows in the current control level and assigns these to the components of the work area wa. In the control levels FIRST, LAST, and outside of an AT-ENDAT control structure, the system calculates the sum of numeric components of all rows in the internal table.
    Example
    Control level processing for creating a list. At the end of line groups, the total of reserved places is calculated and issued.
    DATA: sflight_tab TYPE SORTED TABLE OF sflight
                      WITH UNIQUE KEY carrid connid fldate,
          sflight_wa  LIKE LINE OF sflight_tab.
    SELECT *
           FROM sflight
           INTO TABLE sflight_tab.
    LOOP AT sflight_tab INTO sflight_wa.
      AT NEW connid.
        WRITE: / sflight_wa-carrid,
                 sflight_wa-connid.
        ULINE.
      ENDAT.
      WRITE: / sflight_wa-fldate,
               sflight_wa-seatsocc.
      AT END OF connid.
        SUM.
        ULINE.
        WRITE: / 'Sum',
                  sflight_wa-seatsocc UNDER sflight_wa-seatsocc.
        SKIP.
      ENDAT.
      AT END OF carrid.
        SUM.
        ULINE.
        WRITE: / 'Carrier Sum',
                  sflight_wa-seatsocc UNDER sflight_wa-seatsocc.
        NEW-PAGE.
      ENDAT.
      AT LAST.
        SUM.
        WRITE: / 'Overall Sum',
                  sflight_wa-seatsocc UNDER sflight_wa-seatsocc.
      ENDAT.
    ENDLOOP.
    Hope this solves ur problem....
    <b>do reward if useful....</b>
    regards
    dinesh

  • Different value in the sum in a table of Desktop Intelligence

    Greetings, I created a report in Desktop Intelligence and I created a variable that contains the sum of the table. The table gives a value but when I put the variable in other part of the report give a total different value. When I checked the table contains some duplicate values. I used Avoid Duplicate Row Aggregation but the problem is worst. How can I remove the duplicate values in a row of table ?

    Hi  Miguel Rodriguez,
    what is the meaning of sum of table???
    i didnot  understud.
    aggregation performence is good when you make A object as measure Object in Universe level.
    if you want to like this:
    customer      product_1       product_2      product_3
        A             10                  50               60
        B             20                  60               40
        C             30                  70               20
        D             40                  80               80
    create a variable    PRODUCT
    =Sum( [product_1]  + [product_1] + [product_1])
    customer      product_1       product_2      product_3                    PRODUCT
        A             10                  50               60                   120
        B             20                  60               40                   120
        C             30                  70               20                   120
        D             40                  80               80                   200
    Next Click on Product column     File menu   -
    > Insert -
    > calculations -
    >Sum
    customer      product_1       product_2      product_3                    PRODUCT
        A             10                  50               60                   120
        B             20                  60               40                   120
        C             30                  70               20                   120
        D             40                  80               80                   200
                                                                            Sum= 560
    All the best,
    Praveen

  • Sum from cluster table

    I want to make a SUM for a field from KONV table. Making the sum directly doesn't works because konv is a cluster table. What is the code for making the sum from this table?
    Thank you.

    Hi,
    You can proceed with normal selection process ie : fetch the data into and internal table and then later
    you can loop that particular internal table and collect it into another internal table  to sum up the values.
    for example :
             select * from ( your required field which you want )
                       from konv
                        into table it_konv.
    if it_konv[] is not initial.
         loop at it_konv into wa_konv.
            wa_konv to wa_collect.
    collect wa_collect to it_collect.
    endloop.
    collect statement will full fill your requirment
    Let me know if you need further information
    Regards
    Satish Boguda

  • How to get the sum in BSEG table using select statement

    hai all
    i made the internal tale "itab1" .. i want to get the som  feild of  DMBTR in BSG table my code is here but its not working gave som error massage (Aggregate functions and the addition DISTINCT are not supported in field lists for pooled and cluster tables.)  plz tel me how should i do it..... i want to get the som of that feild....          
    loop at itab1
         SELECT sum( DMBTR  ) from bseg INTO itab1-DMBTR141_45
             where GJAHR = itab1-GJAHR
             and   BELNR = itab1-BELNR.
    endloop.
    regard
    nawa

    SELECT BELNR GJAHR SHKZG DMBTR
                 from bseg
                 INTO table it_bseg
                 for all entries in itab1
                 where GJAHR = itab1-GJAHR
                   and BELNR = itab1-BELNR.
    loop at it_bseg.
        IF it_bseg-shkzg = 'H'.
          it_bseg-dmbtr = it_bseg-dmbtr * ( -1 ).
        ELSE.
          it_bseg-dmbtr = it_bseg-dmbtr.
        ENDIF.
        MODIFY it_bseg.
    endloop.
    loop at it_bseg.
    READ TABLE itab1 with key belnr = it_bseg-belnr
                               gjahr = it_bseg-gjahr.
    if sy-subrc = 0.
    collect it_bseg into it_bseg_amount.
    endif.
    endloop.
    U can use the collect statement
    Regards
    Gopi

  • How to sum from multiple tables on one table?

    I have 12 tables covering 4 separate pages in Numbers, with each of the 12 tables summing each row at the bottom of each table. I'd like to take the sums of each of the 12 tables, and sum each of those sums together into one table. For instance, each table is for a different location, but they each have an "attendance" box. I've summed the attendance for that location for the year, but now I'd like to set a formula so that I can sum ALL ATTENDANCE for ALL LOCATIONS.
    Is there anyway to do that?

    You can easily combine the two functions. You bracket the ROUND "around" the AVERAGE.
    Take your two formulas in their basic forms.
    =AVERAGE(D1:D4) will average cells D1 to D4
    =ROUND(A,2) will round up number A to 2 decimal places.
    Now, we want to round up the result of the Average formula so, replace the A in the ROUND with the entire AVERAGE formula above (cut and paste is good here):
    =ROUND(AVERAGE(D1:D4),2)
    This will first calculate your average and then round it up to 2 decimals.
    If you want to round to the nearest whole number use 0
    Hope this is what you need.
    M.

  • Sum in internal table issue

    Dear Experts,
    I have a Qty field which needs to be summed up when material no, po no and size are same in my internal table
    Eg: PO no     Mat no     Size      Qty
          111          001          L          10
          111          001          L          20
    needs to be shown as
          111          001          L          30
    I have tried using the collect statement in several ways but unfortunately ddnt work out. Any suggestions?

    Although Farid's sugegestion of looping and over two tables and summing would work I don't there is any reason to desist with your original approach of using the collect statement. The collect statment is designed exactly for this purpose and would be more efficient than two nested loops. As described before the folloiwng code should work the correct data elements inserted instead of the descriptions
    types: BEGIN OF tys_structure,
             po_num type po_num,
             mat_no type mat_no,
             size type sizw,
             qty type qty,
            END OF tys_structure.
    data: lt_table type standard table of tys_structure
               with non-unique key matnr, po_no, size,
             ls_workarea type type_structure.
    collect ls_workarea into lt_table.
    One additional note, It would probably be better to use a unique key of a sorted or hashed table if you only want the results to be aggreagted by material no, po no and size. I suggested a non-unique key of a standard table here because it is more flexible and i don't know what elese you might want to do with this table in your code. 

  • Help in doing sum fields from tables

    HALLOW
    i have a table and i wont to add the tables line to one line , to do sum
    if date appear more then one time like in example.
    i give example
    date----
    hours
    01.01.2002 6.5
    01.01.2002 2.5
    02.01.2002 5
    03.01.2002 3
    03.01.2002 3
    04.01.2002 4
    06.01.2002 5
    i wont to move it to table like this
    date----
    hours
    01.01.2002 9 ->sum houres of date 01.01.2002
    02.01.2002
    03.01.2002 6 ->sum houres of date 03.01.2002
    04.01.2002 4
    06.01.2002 5
    Regards
    i reward

    You can get same functionality if you use Internal table events .
    See the simple example :
    *& Report  ZTEST_IEVENTS
    REPORT  ZTEST_IEVENTS no standard page heading
                          line-count 40(2).
    tables : vbap.
    data : begin of i_vbap occurs 0,
           vbeln like vbap-vbeln,
           posnr like vbap-posnr,
           matnr like vbap-matnr,
           kwmeng like vbap-kwmeng,
           netpr like vbap-netpr,
           end of i_vbap.
    data wa_vbap like line of  i_vbap.
    data v_flag type c.
    select-options s_vbeln for vbap-vbeln obligatory.
    start-of-selection.
    select vbeln
           posnr
           matnr
           kwmeng
           netpr from vbap
           into table i_vbap
           where vbeln in s_vbeln.
    sort i_vbap by vbeln posnr.
    end-of-selection.
    loop at i_vbap.
    move i_vbap to wa_vbap.
    at first.
    write:/2 'Order #',15 'Item #',28 'Material #',50 'Qty', 70 'Net value'.
    skip 1.
    endat.
    at new vbeln.
    write:/2 wa_vbap-vbeln,15 wa_vbap-posnr,28 wa_vbap-matnr,
            47 wa_vbap-kwmeng,65 wa_vbap-netpr.
    v_flag = 'X'.
    endat.
    if v_flag ne 'X'.
    write:/15 wa_vbap-posnr,28 wa_vbap-matnr,
            47 wa_vbap-kwmeng,65 wa_vbap-netpr.
    endif.
    at end of vbeln.
    sum.
    skip 1.
    write:/5 'Sub totals', 47 i_vbap-kwmeng,65 i_vbap-netpr.
    skip 1.
    endat.
    at last .
    skip 1.
    sum.
    write:/5 'Grand Totals',47 i_vbap-kwmeng,65 i_vbap-netpr.
    skip 1.
    write:/ 'end of page', 'Footer'.
    endat.
    clear v_flag.
    endloop.
    Good luck
    Thanks
    Seshu

  • Running Sum in Pivot Table

    Hi Experts,
    I have the following pivot table:
    ----------SUM1--SUM2--SUM3
    -------A--20------30-------45
    -------P--10------15--------5
    ----DIFF-10------15-------40
    where the last row is new calculated item. Is it possible to create another calculated row by using value from the 'Diff' row?. What I want to achieve is Running Sum for 'Diff' row.
    So I need an output like:
    ----------SUM1--SUM2--SUM3
    -------A--20------30-------45
    -------P--10------15--------5
    ----DIFF-10------15-------40
    RSUM-10----25-------65
    My BI version is BI EE 10.1.3.4.1. I suppose that some kind of trick will be helped.
    Thanks in advance for any suggestion
    Regards,
    Esk

    Hi, It is possible in pivot view.
    You need to move the measure labels to the left..where it says rows.
    Once that is done, we can click on any of the facts and choose display as running sum

  • Sum in Internal Tables

    Dear Friends,
    I AM WRITING A REPORT TO SHOW THE ALL THE POs WHOSE GRs HAS BEEN DONE BUT NOT THE IRs.
    LIKE :-
    Serial_no    Vendor_no    vendor_name     Po_no         ItemNo       GR_no          Ref_Doc_num          Amount
    I GOT THE REPORT BUT NOW MY USER SAYS ....
    HE WANTS one recod (Serial_no) if po_no and GR_no and ref_doc_num are same with Amount summed up.
    so that serial number will be less and and he will get the total amount of the pending GR.
    I am not getting the logic how shall I sum up the amounts comapring po, gr and Ref doc no.
    Regards,
    jeevan.

    Thanks for your replies. but i am not getting that......
    i have an internal table with POs ..item nos...grs..ref no..... anmount..
    now i have to get it in another internal table where there will be only Serialno .. po.no.gr..and ref and amount summed up all the similar ref numbers.
    here is code.. its not optimized...but see... i have included
    *& Report  ZMMR_TEST2
    REPORT  ZMMR_TEST2.
    TABLES : EKKO, EKBE, LFA1.
    TYPE-POOLS : SLIS.
    DATA : begin of IT_ekbe4 OCCURS 1  ,
            i_index type i,
            lifnr TYPE ekko-lifnr,
            name1 TYPE lfa1-name1,
            no_name type string,
            ebeln TYPE ekbe-ebeln,
            ebelp TYPE ekbe-ebelp,
            belnr TYPE ekbe-belnr,
            xblnr TYPE ekbe-xblnr,
            dmbtr TYPE ekbe-dmbtr,
            bewtp TYPE ekbe-bewtp,
            bwart TYPE ekbe-bwart,
            menge type ekbe-menge,
           end of IT_ekbe4.
    DATA : it_ekbe like ekbe occurs 1 with header line,
          it_ekbe2 like ekbe occurs 1 with header line,
          it_ekbe3 like it_ekbe4 occurs 1 with header line.
    DATA : V_TABIX LIKE SY-TABIX.
    *DATA : it_ekbe4 type standard table of s_ekbe4 initial size 0,
           wa_ekbe4 type s_ekbe4.
    data: fieldcatalog type slis_t_fieldcat_alv with header line,
          gd_tab_group type slis_t_sp_group_alv,
          gd_layout    type slis_layout_alv,
          gd_repid     like sy-repid.
    *selection screen.
    select-options : s_date for ekbe-budat.
    PARAMETERS DOC_TYP LIKE EKKO-BSART.
    *Start-of-selection.
      select a~ebeln a~ebelp a~bewtp a~bwart a~xblnr a~dmbtr a~belnr
                b~lifnr
                c~name1
                into  (it_ekbe4-EBELN, it_ekbe4-ebelp, it_ekbe4-bewtp,
    it_ekbe4-bwart, it_ekbe4-xblnr, it_ekbe4-dmbtr, it_ekbe4-belnr,
    it_ekbe4-lifnr, it_ekbe4-name1)
                from ekbe as a
                inner join ekko as b
                on a~ebeln = b~ebeln
                inner join lfa1 as c
                on b~lifnr = c~lifnr
                where a~budat in s_date
                and a~bewtp = 'E'
                and a~bwart in ('101','102', '122')
                and b~bstyp = 'F'
                and b~bsart  = DOC_TYP
                and b~ekorg = '1000'
         select ebeln ebelp bewtp bwart
                  from ekbe
                  into (it_ekbe2-EBELN , it_ekbe2-ebelp , it_ekbe2-bewtp ,
    it_ekbe2-bwart)
                  where ebeln = IT_EKBE4-EBELN
                  and ebelp = it_ekbe4-ebelp
                  and bewtp = 'Q'.
          APPEND IT_EKBE2.
        ENDSELECT.
         APPEND IT_EKBE4.
      ENDSELECT.
      LOOP AT IT_EKBE4.
        V_TABIX = SY-TABIX.
        READ TABLE IT_EKBE2 WITH KEY EBELN = IT_EKBE4-EBELN ebelp =
    it_ekbe4-ebelp.
        IF SY-SUBRC = 0.
          DELETE IT_EKBE4 INDEX V_TABIX.
        ENDIF.
        ENDLOOP.
        clear v_tabix.
      LOOP AT IT_EKBE4.
        if ( it_ekbe4-bwart = '102' or it_ekbe4-bwart = '122' ).
          it_ekbe4-dmbtr = it_ekbe4-dmbtr * -1.
        endif.
           it_ekbe3-i_index = sy-tabix.
           it_ekbe3-ebeln = it_ekbe4-ebeln.
           it_ekbe3-ebelp = it_ekbe4-ebelp.
           it_ekbe3-belnr = it_ekbe4-belnr.
           it_ekbe3-xblnr = it_ekbe4-xblnr.
           it_ekbe3-dmbtr = it_ekbe4-dmbtr.
           it_ekbe3-lifnr = it_ekbe4-lifnr.
           it_ekbe3-name1 = it_ekbe4-name1.
           concatenate it_ekbe4-name1 '/' it_ekbe4-lifnr into  it_ekbe3-no_name.
    *separated by space.
           append it_ekbe3. "YOU TOLD THIS TO BE COLLECT."
          Collect it_ekbe3 into it_ekbe3.
      ENDLOOp.
    sub totals.
    *loop at it_ekbe3.
    *collect
    *endloop.
    loop at it_ekbe3.
    write:/ it_ekbe3-ebeln , 'gr', it_ekbe3-belnr , 'ref', it_ekbe3-xblnr, 'amount', it_ekbe3-dmbtr.
    endloop.

  • Show name from one table, and a sum from another table.

    Hi,
    For instance: I have 2 tables.
    1. Table1 has fields TenantID, TenantName,
    2. Table2 has fields AmmountCredit, AmmountDebit, TenantID.
    I need the following behavior.
    In the detailed section I want my report to show a list of TenantName, and a SUM of AmmountCredit minus Ammount Debit for that Tenant (which will come from Table2).
    How would I do this?  If a formula is involved, please provide an example because I'm fairly new to Crystal Reports.
    Thanks
    Aron

    Hi Aron,
    Please see if this helps:
    1) Go to the Database Expert > Add the two tables > Go to the Links tab > Join the two tables on the 'Tenant ID' field using an 'Inner Join' > Click OK
    2)  In the report, go to the Group Expert and create a group on the 'Tenant ID' field
    3) Place the 'Tenant Name' field on the Group Header 1
    4) Suppress the Details Section
    5) Create a formula with this code and place it on the Group Header 1:
    Sum({AmountCredit}, {TenantID}) - Sum({AmountDebit}, {TenantID})
    Here, {TenantID} is the Group field. So replace these fields with the right fields from your datasource.
    -Abhilash

  • Best way to select from 2 tables, based on sum from detail table

    I have a "customer order line detail" table from which I want to report
    Order Number
    Customer Number
    Part Number
    Line Value { which is Unit Qty * (Unit price - discount%) }
    But only for orders which are above £1500.
    And lines which are not "Cancelled"
    I have access to an API which returns the total order value for the order.
    I am currently using this in my criteria, eg:
    select order_no, customer_no, part_no, round(unit_qty *(unit_price - unit_price *(discount/100)),2) Line_value
    from customer_detail
    where
    status != 'Cancelled'
    and
    Customer_Order_API.Get_Total_Sale_Price__(order_no)>=1500
    The API contains the following:
    SELECT SUM(ROUND((qty * price_conv_factor * unit_price), rounding_) -
    ROUND((qty * price_conv_factor * unit_price) -
    ((qty * price_conv_factor * unit_price) * ((1 - discount / 100) * (1 - order_discount / 100))), 2))
    FROM customer_detail
    WHERE order_no = order_no_
    AND state != 'Cancelled'
    AND line_item_no <= 0
    (that uses a price conversion factor that I don't use, since it's always 1)
    My query runs so slowly, because it is getting the order value for every line of the order, is it possible to improve?
    Thanks

    Thanks for the suggestion, it was close to what I needed.
    I ended up with this:
    select order_no,  name, ref_id, c_salesman_sub_division, line_value, catalog_desc
    from
    (SELECT coj.customer_no, coj.order_no, ifsapp.customer_info_api.GET_NAME(customer_no) name, coj.ref_id,
           coj.c_salesman_sub_division, ROUND((coj.buy_qty_due * coj.
           price_conv_factor) * coj.sale_unit_price, 2) - ROUND((coj.buy_qty_due *
           coj.price_conv_factor) * coj.sale_unit_price - ((coj.buy_qty_due * coj.
           price_conv_factor) * coj.sale_unit_price) * ((1 - coj.discount / 100) *
           (1 - coj.order_discount / 100)), 2) line_value, coj.catalog_desc,
    SUM( ROUND((coj.buy_qty_due * coj.
           price_conv_factor) * coj.sale_unit_price, 2) - ROUND((coj.buy_qty_due *
           coj.price_conv_factor) * coj.sale_unit_price - ((coj.buy_qty_due * coj.
           price_conv_factor) * coj.sale_unit_price) * ((1 - coj.discount / 100) *
           (1 - coj.order_discount / 100)), 2)) OVER (PARTITION BY Coj.ORDER_NO) AS SUM_ORDER
        FROM ifsapp.customer_order_join coj
        WHERE coj.order_no = coj.order_no
          AND TRUNC(coj.date_entered) = NVL(TO_DATE('25/01/2007', 'DD/MM/YYYY'),
              TRUNC(SYSDATE))
          AND coj.authorize_code = 'UKMEDM'
          AND coj.line_item_no >= 0
          AND coj.ref_id <> 'SAMP'
          AND coj.state <> 'Cancelled'
    where sum_order >=1500
        ORDER BY ref_id, c_salesman_sub_division, customer_no, order_no
    /[pre]
    But I have realised the problem with this.  Not sure if I made it clear, to myself even, but I need to include
    order lines added on a particular day (that are not cancelled)
    for orders which total >= £1500.
    If one line of an order is added for £900 but the rest of the order is £2000 then that line should be shown.
    My SQL only shows lines added for the day >= 1500.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Sum of four tables in dynamic form

    There will be a number entered in the last cell of each row which will total at the bottom. I've read and tried numerous ways to total the tables and none work for me. What am I doing wrong?

    on the calculate event of the field you want to show the total, enter this code.
    sum (Field1, Field2, Field3, Field4)
    replace the "Field" parts with the desired fields. when entering the code ctrl+click the sections you want to calculate and separate them with a comma. This will get the absolute name for the objects to be referenced.

  • Sum in joined tables

    Hello,
    I have a master table:
    t0  t1   t2    t3
    1   d    1,81  700
    2   d    1.81  800
    and a detail table (t0 e tb0 colums joined)
    tb0  tb1 tb2   tb3
    1   A   100   0
    1   A   200   0
    1   B   500   0
    2   B   500   0
    How can I get this following output?
    d     3,62    1500    A  300    0    
                          B  1000   0because with
    select t1, sum(t2), sum(t3), tb1, sum(tb2), sum(tb3) from table1 a, table2 b
    where    a.t0 = b.t0
    group by t1, tb1I have not success
    Thanks for any help

    Well as you said you weren't worried about breaking as you'd do that in the report, I've just done the basic requirement here:
    SQL> ed
    Wrote file afiedt.buf
      1  with master as (select 1 t0, 'd' t1, 1.81 t2, 700 t3 from dual union all
      2                  select 1 t0, 'd' t1, 1.81 t2, 300 t3 from dual union all
      3                  select 2 t0, 'd' t1, 1.81 t2, 800 t3 from dual)
      4      ,detail as (select 1 tb0, 'A' tb1, 100 tb2, 0 tb3 from dual union all
      5                  select 1 tb0, 'A' tb1, 200 tb2, 0 tb3 from dual union all
      6                  select 1 tb0, 'A' tb1, 500 tb2, 0 tb3 from dual union all
      7                  select 1 tb0, 'B' tb1, 200 tb2, 0 tb3 from dual union all
      8                  select 1 tb0, 'B' tb1, 200 tb2, 0 tb3 from dual union all
      9                  select 2 tb0, 'B' tb1, 500 tb2, 0 tb3 from dual)
    10  --
    11  select m.t0, m.sum_t2, m.sum_t3, d.tb1, d.sum_tb2, d.sum_tb3
    12  from (
    13        select t0
    14              ,sum(t2) as sum_t2
    15              ,sum(t3) as sum_t3
    16        from master
    17        group by t0
    18       ) m
    19  join (
    20        select tb0
    21              ,tb1
    22              ,sum(tb2) as sum_tb2
    23              ,sum(tb3) as sum_tb3
    24        from detail
    25        group by tb0, tb1
    26       ) d
    27  on (m.t0 = d.tb0)
    28* where m.t0 = 1
    SQL> /
            T0     SUM_T2     SUM_T3 T    SUM_TB2    SUM_TB3
             1       3.62       1000 A        800          0
             1       3.62       1000 B        400          0
    SQL>

  • Comparing sum from two tables and correct by difference of sum in second t,

    Hello Guys,
    I have a very tricky task that I cant get my head around.
    The sample data looks like that:
    Master Table
    Claim------Booking--------Debit--------Credit------------MasterAmout          
    1------------1----------------D---------------------------------15.3          
    1------------2----------------D---------------------------------480.6          
    1------------3------------------------------C-------------------496.8          
    ------------------------------------------- 0.9          
    Slave Table
    Claim-----Booking-----Debit------------Credit----------SlaveAmout------- SlaveCorrection
    1------------1------------D------------------------------------15.3---------------14.5
    1------------2------------D------------------------------------480.6-------------480.6
    1------------3-----------------------------C-------------------496---------------496
    -------------------------------------------0.1--------------------------------------0.9
    The booking have a total amout of 0.1 but has to be corrected to 0.9 because the master table has 0.9.
    Booking 1 requires a correction so the slave table also has a total of 0.9 (business rule is, corrections only on first booking). so we have
    to change the amount from 15.3 to 14.5. I plan my SQL like that:
    1. Sum bookings from both tables for each Claim. Compare the two for each individual claim.
    2. If the Master sum of the amount shows a difference between the master and slave table
         2.1 select top 1 booking from slave table for the specific claim and increase/decrease by the difference of those two amounts.
    Does that make sense?

    Hello
    It would be really helpful if you could a) format your posting properly using the {noformat}{noformat} tags around your data and b) provide create table/ insert statements.
    Anyway, from what you described -
    Booking 1 requires a correction so the slave table also has a total of 0.9 (business rule is, corrections only on first booking). so we haveto change the amount from 15.3 to 14.5
    You could achieve this with an analytic function and an outer join...WITH Master_Table AS
    SELECT 1 Claim, 1 booking, 'D' debit,NULL credit,15.3 masteramount from dual union all
    SELECT 1,2,'D',NUll,480.6 from dual union all
    SELECT 1,3,NULL,'C',496.8 from dual
    Slave_Table AS
    ( SELECT 1 claim, 1 booking, 'D' debit, null credit, 15.3 slaveamount,14.5 slavecorrection from dual union all
    SELECT 1,2,'D',NULL,480.6,480.6 from dual union all
    SELECT 1,3,null,'C',496,496 from dual
    SELECT
    m.claim,
    m.booking,
    m.debit,
    m.credit,
    NVL(s.slavecorrection,m.masteramount) amount
    FROM
    master_table m
    LEFT OUTER JOIN
    ( SELECT
    claim,
    booking,
    slaveamount,
    slavecorrection,
    ROW_NUMBER() OVER(PARTITION BY claim ORDER BY booking) rn
    FROM
    slave_table s
    ) s
    ON
    ( s.claim = m.claim
    AND
    m.booking = s.booking
    AND
    s.rn = 1
    CLAIM BOOKING D C AMOUNT
    1 1 D 14.5
    1 2 D 480.6
    1 3 C 496.8
    HTH
    David                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

Maybe you are looking for