‎2007 Mar 26 7:57 PM
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.
‎2007 Mar 26 8:19 PM
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
‎2007 Mar 26 8:01 PM
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
‎2007 Mar 26 8:04 PM
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
‎2007 Mar 26 8:19 PM
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