Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

SUM(Internal Table)

Former Member
0 Likes
616

Dear All

i have one internal table . 24 fields are avilable in that table . i want the sum for all fields.

How to get total?

Thanks and Regards

Suresh

5 REPLIES 5
Read only

Former Member
0 Likes
582

using control command you can sum the field if they are neumaric. declare the itab1 of same structure of itab.

Loop at itab into <workarea>.

At end of <Field>

sum.

append <waarea> to itab1.

atend.

endloop.

Read only

Former Member
0 Likes
582

Hi,

Check the following example:

DATA: BEGIN OF LINE,

COL1 TYPE C,

COL2 TYPE I,

COL3 TYPE I,

END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE

WITH UNIQUE KEY COL1 COL2.

LINE-COL1 = 'A'.

DO 3 TIMES.

LINE-COL2 = SY-INDEX.

LINE-COL3 = SY-INDEX ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

LINE-COL1 = 'B'.

DO 3 TIMES.

LINE-COL2 = 2 * SY-INDEX.

LINE-COL3 = ( 2 * SY-INDEX ) ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

SORT ITAB.

LOOP AT ITAB INTO LINE.

WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.

AT END OF COL1.

SUM.

ULINE.

WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.

SKIP.

ENDAT.

AT LAST.

SUM.

ULINE.

WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.

ENDAT.

ENDLOOP.

Regards,

Bhaskar

Read only

matt
Active Contributor
0 Likes
582

Using obsolete ADD-CORRESPONDING.

DATA: wa_sum LIKE LINE OF itab.
FIELD-SYMBOLS: <wa> LIKE LINE OF itab.
LOOP AT itab ASSIGNING <wa>.
  ADD-CORRESPONDING <wa> TO wa_sum.
ENDLOOP.

Use collect.

DATA: itab_sum ... " defined as table of same type as itab
LOOP AT itab ASSIGNING <wa>.
  COLLECT <wa> INTO itab_sum.
ENDLOOP.

itab_sum now contains the sum of all the numeric fields for all the character field combinations of your internal table.

matt

Read only

Former Member
0 Likes
582

Hi,

You can use the collect statement.

Assume you have internal table itab with data.

Do as below :

Loop at itab into wa.

collect wa to itab2.

endloop.

or 

loop at itab.

at end of field.
sum.
endat.
endloop.

Thanks,

Sriram Ponna.

Read only

0 Likes
582

hi sriram

Thanks.

pls one example

Thanks

Suresh