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

adding the numeric fields in internal table

Former Member
0 Likes
1,175

Hi guys,

i have data like this:

data1 data2 data3

1001(type c) 2 2568 (type c)

1001(type c) 3 2569 (type c)

For every same entry in data1, i have to sum the correponding data in data2. how can it be done?

thanks a lot!

rgds,

mark

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
813

hi there....

try this out....

loop at itab.

at new itab-data1.

sum = 0.

break.

endat.

sum = sum + itab-date2.

write 😕 sum.

endloop.

here sum is integer type and initialized to zero.

everytime the data1 value changes, the looop breaks and control again goes back to looop at command. this way, for same data1 values, sum will get calculated and stored in sum.

i hope it helps...

do reward if helpful or get back for further assistance.

5 REPLIES 5
Read only

former_member216668
Participant
0 Likes
813

Use COLLECT statement

Read only

andreas_mann3
Active Contributor
0 Likes
813

use commands:

1) add-corrresponding

or 2) collect

A.

Read only

former_member216668
Participant
0 Likes
813

An example for COLLECT statement

DATA: BEGIN OF LINE,
COL1(3) TYPE C,
COL2(2) TYPE N,
COL3 TYPE I,
END OF LINE.
DATA ITAB LIKE SORTED TABLE OF LINE
WITH NON-UNIQUE KEY COL1 COL2.

LINE-COL1 = 'abc'. LINE-COL2 = '12'. LINE-COL3 = 3.
COLLECT LINE INTO ITAB.
WRITE / SY-TABIX.
LINE-COL1 = 'def'. LINE-COL2 = '34'. LINE-COL3 = 5.
COLLECT LINE INTO ITAB.
WRITE / SY-TABIX.
LINE-COL1 = 'abc'. LINE-COL2 = '12'. LINE-COL3 = 7.
COLLECT LINE INTO ITAB.
WRITE / SY-TABIX.
LOOP AT ITAB INTO LINE.
WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.
ENDLOOP.

The output is:

1

2

1

abc 12 10

def 34 5

Read only

0 Likes
813

Hi guys,

thnx for your responses,,, but im not sure collect will work for this since i have two fields with type c,,, and i only consider the same entry for data1 to sum up values in data2..

tnx again...

Read only

Former Member
0 Likes
814

hi there....

try this out....

loop at itab.

at new itab-data1.

sum = 0.

break.

endat.

sum = sum + itab-date2.

write 😕 sum.

endloop.

here sum is integer type and initialized to zero.

everytime the data1 value changes, the looop breaks and control again goes back to looop at command. this way, for same data1 values, sum will get calculated and stored in sum.

i hope it helps...

do reward if helpful or get back for further assistance.