Application Development 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: 

SUM(Internal Table)

Former Member
0 Kudos
79

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

Former Member
0 Kudos
45

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.

Former Member
0 Kudos
45

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

matt
Active Contributor
0 Kudos
45

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

Former Member
0 Kudos
45

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.

0 Kudos
45

hi sriram

Thanks.

pls one example

Thanks

Suresh