Skip to Content
avatar image
Former Member

addition of fields

hi,

i want to add contents of field 'F1' depending upon the field 'F2' of same table, such that.. sum of 'F1' values of all records whose value of field 'F2' is same

thank you..

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

6 Answers

  • avatar image
    Former Member
    Oct 17, 2008 at 07:48 AM

    Hello,

    Use COLLECT statement so that you will get the sum.

    But the condition is the internal table must be only 2fields.

    ie in this order 1st field must be F2 , F1

    COLLECT y_wa_coll INTO y_i_coll.

    CLEAR y_wa_coll.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 17, 2008 at 07:55 AM

    hi,

    suppose ur internal table structure is F2 F1 ......

    ur requirement is that add the values of F1 of all the records where F2 is same.

    for this sort the internal table and loop at the internal table and use AT END OF F2.

    like:

    data val1 type F1.

    data val2 type F1.

    data res type F1.

    sort <internal table> ascending by F2 F1.

    loop at < internal table>.

    val1 = <internal table>-F1.

    res = val1 + val2.

    at end of F2.

    move res to <new internal table>

    append <new internal table>.

    endat.

    endloop.

    this will work...

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 17, 2008 at 08:29 AM

    Hi,

    suppose two internal tables are I1 & I2.

    sum = 0.

    loop at I1.

    read table I2 with key field1 = I1-field1.

    if sy-subrc = 0.

    sum = sum + I2-field1.

    endif.

    endloop.

    write : / sum.

    thanks,

    Krishna

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 17, 2008 at 12:23 PM

    If you may build your internal table as you want, put F2 as first column, and use following code:

    SORT ITAB BY F2.
    LOOP AT ITAB.
    AT END OF F2.
      SUM.
    * FI CONTAINS NOW THE SUM OF F1 FOR ALL ROWS WITH THE SAME VALUE IN F2
      ...
    ENDAT.
    ...
    ENDLOOP.
    

    If not possible, use COLLECT statement with an additional table, as in the first answer.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 17, 2008 at 12:30 PM

    Hi,

    If Field f2 is a Character field , then have internal table first field as f2 and second as f1.

    Now do this..

    data: itab1 like itab with headerline.

    Loop at itab.

    move itab to itab1 .

    collect itab1.

    endloop.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 17, 2008 at 12:44 PM

    Please note that with COLLECT statement, the sequence of the fields has no importance. It is more readable to put the key field(s) as first field(s), but not mandatory. With the AT END statement, you must have the key fields as first fields.

    Add comment
    10|10000 characters needed characters exceeded