Skip to Content
author's profile photo Former Member
Former Member

to sum values in a internal table

hi

iam using below loop

Loop at it_table.

At end of amount.

sum.

endat.

At end of field2.

sum.

endat.

endloop.

my purpose is to sum all values of the table field1 and field2 and use it to display please let me know how to do it

regards

arora

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Jun 16, 2008 at 03:33 PM

    Hi Arora,

    SUM in control break command will hold sum of the variable you will mention with control command. If you want to add sum of two variables then add totals of each SUM varaible.

    Check the below code.

    V_FIELD1_SUM
    V_FIELD2_SUM
    V_TOTAL_SUM
    
    Loop at it_table.
    
    At LAST amount.
    sum.
    WRITE IT_TABLE-amount TO V_FIELD1_SUM.
    endat.
    
    At LAST field2.
    sum.
    WRITE IT_TABLE-field2 TO V_FIELD2_SUM.
    endat.
    
    
    endloop.
    V_TOTAL_SUM = V_FIELD1_SUM + V_FIELD2_SUM.
    WRITE:/ V_FIELD1_SUM,V_FIELD2_SUM,V_TOTAL_SUM
    

    Thanks,

    Vinay

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      thanks vinay got it

      so i have to do at last for each field right

      cant it be i loop and just use at last and initnernal table for all values the sum happens as all fields in internal table i need to sum up

      or is there any option to use collect can we use that collect?

  • author's profile photo Former Member
    Former Member
    Posted on Jun 16, 2008 at 07:16 AM

    Hi Nishant,

    If u want to display the grand total (at last of all the items)

    then use

    AT LAST.
    SUM.
    ENDAT.

    If u want to display the totals line item wise.

    then use

    AT END OF <field_name>.
    SUM.
    ENDAT.

    Best regards,

    raam

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Hi Nishant,

      As u said there are only two fields in the internal table.

      If you use SUM in an AT - ENDAT block, the system calculates totals for the numeric fields of all lines in the current line group and writes them to the corresponding fields in the work area.

      So the both fields of ur internal table should be of Numeric Type, then only the SUM statement works .

      Use

      AT LAST.
      SUM.
      ENDAT

      .

      Best regards,

      raam

  • Posted on Jun 16, 2008 at 07:17 AM

    sum1 = 0.

    sum2 = 0.

    Loop at it_table.

    sum1 = sum1 + field1.

    sum2 = sum2 +field2.

    endloop.

    write : sum1.

    write : sum2.

    Hope it works...Reward points if useful....

    Regards

    Rudra

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.