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

Former Member
0 Likes
612

Hello friends,

I have 5 rows in my Internal table.

the 4th and 5th is quantity field.

I need to sum up the 4th and 5th field for same first 3 fields, so I am sorting and using on change of statement and is working good.

I was wondering if I can use a sum statement for better performance.

Madhu.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
579

As far as performance is concern, using the sum statement inside loop is always better.

As its defined for this perpose by SAP.

You can also use :

LOOP AT ITAB INTO WA_ITAB.

AT FIRST.

SUM.

{ the fields u want to put for addition at once for whole internal table }

ENDAT.

also

AT END OF CODE.

SUM.

{ at loop wise WA addition }

ENDAT.

ENDLOOP.

hope this ll be helpful.

Thank-You.

Regards,

vinsee

3 REPLIES 3
Read only

suresh_datti
Active Contributor
0 Likes
579

In oder to use SUM, you have to open the loop, so the performance diff would be marginal between onchange of & the AT END OF control breaks (inside which you can SUM the values). The other option is to COLLECT the itab insteadof APPENDing.

~Suresh

Read only

Former Member
0 Likes
579

Hi Madhu,

Yes, You are correct and You can use SUM statement for better performance.

SORT the internal table on the three fields.

LOOP the intenrnal table and use SUM to get the total AT END OF control command.

Otherwise, You can go for COLLECT statement as specified by Suresh, COLLECT will sum up of the numeric fields in the internal table if all other non-numeric fields of the internal table are same.

SORT ITAB BY F1 F2 F3.

LOOP AT ITAB.

AT END OF F3.

SUM.

ENDAT.

(OR)

COLLECT ITAB INTO ITAB1.

ENDLOOP.

Thanks,

Vinay

Read only

Former Member
0 Likes
580

As far as performance is concern, using the sum statement inside loop is always better.

As its defined for this perpose by SAP.

You can also use :

LOOP AT ITAB INTO WA_ITAB.

AT FIRST.

SUM.

{ the fields u want to put for addition at once for whole internal table }

ENDAT.

also

AT END OF CODE.

SUM.

{ at loop wise WA addition }

ENDAT.

ENDLOOP.

hope this ll be helpful.

Thank-You.

Regards,

vinsee