‎2006 Jun 23 12:49 PM
Hi everyone
I need to calculate some totals in an itab and have discivered the SUM statement, which seems a very easy way to achieve this. However, being a cynical chap I was wondering if this carries a large overhead and will be less efficient than just storing a running total in a variable i.e x = x + y.
I anybody able to shed any light...?
Thanks
Andy
‎2006 Jun 23 12:53 PM
SUM.
Effect
When processing an internal table in a block starting with LOOP and concluded by ENDLOOP , SUM calculates the control totals of all fields of type I , F and P (see also ABAP/4 number types ) and places them in the LOOP output area (header line of the internal table or an explicitly specified work area).
Sum statement in report is basically used in Control Break Processing.
After you fill an internal table with data, you often need to write the data out. This output will frequently contain summary information(uch as totals) at the top or bottom of the report.There might also be interim situations (such as subtotals ) within the body of report.
To do this , you can read the data into an internal table and then, within loop at , use the following statements:
1. at first / endat
2.at last / endat
3.at new / endat
4.at end of / endat
5.sum
6.on change of / endon.
The first statement of each of these pairs - except for Sum - controls when the code lies between them is executed.This type of control is called a control break.Their purpose is to ececute the code between them whenever a specific condition in the data is detected during the processing of the loop.
Using the Sum statement :
syntax :
at first/last/new/end of.
....
sum
....
endat.
where :
... represents any number of lines of code.
Sum calculates a total for the current value of the control level that contains it.
It finds all rows that have the same values within the control level field and all fields of the left of it.
It sums each numeric column to the right of the control level.
It places the totals in the corresponding fields of the work area.
‎2006 Jun 23 12:53 PM
SUM.
Effect
When processing an internal table in a block starting with LOOP and concluded by ENDLOOP , SUM calculates the control totals of all fields of type I , F and P (see also ABAP/4 number types ) and places them in the LOOP output area (header line of the internal table or an explicitly specified work area).
Sum statement in report is basically used in Control Break Processing.
After you fill an internal table with data, you often need to write the data out. This output will frequently contain summary information(uch as totals) at the top or bottom of the report.There might also be interim situations (such as subtotals ) within the body of report.
To do this , you can read the data into an internal table and then, within loop at , use the following statements:
1. at first / endat
2.at last / endat
3.at new / endat
4.at end of / endat
5.sum
6.on change of / endon.
The first statement of each of these pairs - except for Sum - controls when the code lies between them is executed.This type of control is called a control break.Their purpose is to ececute the code between them whenever a specific condition in the data is detected during the processing of the loop.
Using the Sum statement :
syntax :
at first/last/new/end of.
....
sum
....
endat.
where :
... represents any number of lines of code.
Sum calculates a total for the current value of the control level that contains it.
It finds all rows that have the same values within the control level field and all fields of the left of it.
It sums each numeric column to the right of the control level.
It places the totals in the corresponding fields of the work area.
‎2006 Jun 23 2:25 PM
Hi Kishan
Thanks for replying, however, I already know how the SUM statement works. I'm interested in whether or not this is the most efficient method of summing values in an itab - essentially whether:
LOOP AT itab INTO wa.
SUM.
ENDLOOP.Is more efficient than:
LOOP AT itab INTO wa.
total = total + wa-value.
ENDLOOP.Or vice versa.
Does the size of the itab or other factors have any influence over which is better?
Thanks
Andrew
‎2006 Jun 23 2:31 PM
Hi andrew,
1. If its just a matter of totalling one or few
fields of ther internal table,
then 2nd option will be more efficient.
2. BCOS
3. The first option involves CONTROL-BREAK PROCESSING (which is carried out internally)
(apart from the addition computation which it
does using SUM)
4. The 2nd option is straight-forward
arithmetic computation, which is always faster.
regards,
amit m.
‎2006 Jun 23 2:35 PM
What I do when I want to know something like this is to simply write a small program that does lots of loop passes and compare the results. You can use " GET RUN TIME FIELD f" for the comparison.
Rob
‎2006 Jun 23 2:39 PM
Hello Andrew,
I assume that there are other uses for the internal table in your program and that you are not doing a SELECT just to be able to sum up the values.
Now here's what I would do -
1. I would prefer to use the SUM statement if there's only one numeric field in the internal table. Otherwise the other numeric fields will be summed up as well.
2. In case option 1 fails , I do the summing manually.
3. And lastly, I would see if I can directly do a COUNT( * ) from the database table if the number of entries is huge. I know that this would be an unconventional idea, but I have often found it to be performant.
Regards,
Anand Mandalika.
‎2006 Jun 23 3:10 PM
Thanks Amit
So, would I be correct in thinking that option two is better when control-break processing is not used but option 1 would be better when I am doing control-break processing?
Kind regards
Andrew
‎2006 Jun 23 3:13 PM
Hi again,
1. but option 1 would be better when I am doing control-break processing
Yes u are right.
2. At the same time,
even if there are 1000 to 1 lakh record,
Loop at the internal table may take some time,
3. but a simple arithmetic computation,
in between this loop,
will not affect performance noticeably.
(it will almost be same as without the computation)
regards,
amit m.
‎2006 Jun 23 2:29 PM
‎2006 Jun 23 2:39 PM
Hi,
The keyword SUM should be used when we want to group the data and then find the sum of each group.
You can do this using AT NEW.... in the LOOP
If sum of all the records is needed then '+' should be used.
Depending upon the requirement you should decide to use SUM or '+'
Sameena
Message was edited by: sameena attarwala