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 fields in internal table

Former Member
0 Likes
1,455

Hi,

my sorted table with unique key field1 field2 is defined like:

field1 keyfield

field2 keyfield

field3

field4

i would like to get a sum of field3 and field4 of all entries "for each new field1 field2" and write it out.

field1 = 1

field2 = 100

sum(field3)

sum(field4)

write it out.

field1 = 1

field2 = 200

sum(field3)

sum(field4)

write it out.

field1 = 2

field2 = 100

sum(field3)

sum(field4)

write it out.

Q: Should I use LOOP and AT NEW FIELD1/FIELD2 or can i use another way for that ?

Thanks,

Gordon

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,418

Hi,

my sorted table with unique key field1 field2 is defined like:

field1 keyfield

field2 keyfield

field3

field4

create one more table similar to the above table

loop the first table.

secondtable-field1 = firsttable-field1.

secondtable-field2 = firsttable-field1

secondtable-field3 = firsttable-field1

secondtable-field4 = firsttable-field1

collect secondtable.

endloop.

Regards

MD

14 REPLIES 14
Read only

Former Member
0 Likes
1,418

Hi,

You can use loop and At new.it is enough.

Regards

Jana

Read only

Former Member
0 Likes
1,419

Hi,

my sorted table with unique key field1 field2 is defined like:

field1 keyfield

field2 keyfield

field3

field4

create one more table similar to the above table

loop the first table.

secondtable-field1 = firsttable-field1.

secondtable-field2 = firsttable-field1

secondtable-field3 = firsttable-field1

secondtable-field4 = firsttable-field1

collect secondtable.

endloop.

Regards

MD

Read only

Former Member
0 Likes
1,418

Hi

loop at itab.

res = res + itab1-field3 + itab-field4.

endloop.

Read only

Former Member
0 Likes
1,418

Hello

Do Following:

1. Sort Internal table by Field1 Field2

2. Do Loop... At New Field2... Sum... Endat.... Endloop.

Thanks

Amol Lohade

Read only

Former Member
0 Likes
1,418

declare an internal table same as itab...

first do a sort on internal table...

sort itab by field1 field2.

loop at itab.

itab1-field1 = itab-field1.

itab1-field2 = itab-field2.

itab1-field3 = itab-field3.

itab1-field4 = itab-field4.

collect itab1.

endloop.

loop at itab1.

write:/itab1-field1, itab1-field2, itab1-field3, itab1-field4.

endloop.

Read only

Former Member
0 Likes
1,418

hi,

you will have to use LOOP AT and AT NEW field2.

Read only

0 Likes
1,418

thanks for your help.

what is the better way to avoid performance issue.

1. loop / at new field or

2. second itab and use collect itab.

note: my table is selected from VBRP (invoice items) - very huge

Read only

0 Likes
1,418

Hi

The second option is to be avoided.

2. second itab and use collect itab.

Regards

Pavan

Read only

0 Likes
1,418

that means,

i should use the loop option for this task ?

Read only

Former Member
0 Likes
1,418

hi

try to use COLLECT it will help.

Regards

Sachin

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
1,418

if field1 and field2 are non numeric then

collect those values to other itab...

Read only

0 Likes
1,418

they are both numeric.

Read only

0 Likes
1,418

hi,

then you cannot use "collect" statement.

you will have to first sort table based on field1 field2.

then you will have to use LOOP AT and AT NEW field2.

Read only

0 Likes
1,418

Then do it like this.

loop at itab into wa.

at new field2.

move wa-field1 to it2-field1.

move wa-field2 to it2-field2.

endat.

at end of field2.

sum.

move wa-field3 to wa1-field3.

move wa-field4 to wa1-field4.

append wa1 to it2.

endat.

endloop.