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

summing up fields

Former Member
0 Likes
1,145

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?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,124

hi there

i had gone through a similar problem.collect will not work for your case since there are 2 non-numeric fields...

the foll worked for me...

loop at itab.

at new itab-field1.

sum = 0.

break.

endat.

sum = sum + itab-field2.

write 😕 sum.

endloop.

everytime the field1 value changes, the loop breaks and control goes to loop-at command. this way, for same field1 values, sum will get calculated and stored in sum.

i hope it helps...

reward if helpful

regards

niharika

u can either use collect statement or control break statements like AT END OF...ENDAT.

pk

9 REPLIES 9
Read only

Former Member
0 Likes
1,124

use add-corresponding

Read only

Former Member
0 Likes
1,124

u can either use collect statement or control break statements like AT END OF...ENDAT.

pk

Read only

0 Likes
1,124

hi prashant,

PS that collect wont work properly as there are two fields of type c.

anything else??

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
1,124

loop at itab.

at new field1.

sum.

write itab-field1.

write itab-field2.

endat.

endloop.

Read only

pranav_nagpal2
Contributor
0 Likes
1,124

Hi,

Use collect statement it will be do.

Thanks.

Read only

Former Member
0 Likes
1,125

hi there

i had gone through a similar problem.collect will not work for your case since there are 2 non-numeric fields...

the foll worked for me...

loop at itab.

at new itab-field1.

sum = 0.

break.

endat.

sum = sum + itab-field2.

write 😕 sum.

endloop.

everytime the field1 value changes, the loop breaks and control goes to loop-at command. this way, for same field1 values, sum will get calculated and stored in sum.

i hope it helps...

reward if helpful

regards

niharika

Read only

Former Member
0 Likes
1,124

Hi Prem..

use at-new .... endat with the field name(Data1) and take a temp variable to calculate the DATA2.

Hope this info will work.

Reward Points if helpful

Regards

Karan

Read only

0 Likes
1,124

thanks all....

query solved.... points rewarded to helpful ans...

Read only

pranav_nagpal2
Contributor
0 Likes
1,124

Hi Prem,

First sort internal table by data1 field.

Using

sort itab by data1.

then use this code.

loop at itab.

at new itab-data1.

sum = 0.

break.

endat.

sum = sum + itab-data2.

write 😕 sum.

endloop.