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

Former Member
0 Kudos

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

Former Member
0 Kudos

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

Former Member
0 Kudos

Hi,

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

Regards

Jana

Former Member
0 Kudos

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

Former Member
0 Kudos

Hi

loop at itab.

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

endloop.

Former Member
0 Kudos

Hello

Do Following:

1. Sort Internal table by Field1 Field2

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

Thanks

Amol Lohade

Former Member
0 Kudos

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.

Former Member
0 Kudos

hi,

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

0 Kudos

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

0 Kudos

Hi

The second option is to be avoided.

2. second itab and use collect itab.

Regards

Pavan

0 Kudos

that means,

i should use the loop option for this task ?

Former Member
0 Kudos

hi

try to use COLLECT it will help.

Regards

Sachin

kesavadas_thekkillath
Active Contributor
0 Kudos

if field1 and field2 are non numeric then

collect those values to other itab...

0 Kudos

they are both numeric.

0 Kudos

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.

0 Kudos

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.