‎2008 May 16 7:48 AM
Loop at itab into <wa>
Here I want to sum all <wa>-field2
Can I use
compute sum = sum + <wa-field2>
Endloop
is there a more elegant method
‎2008 May 16 7:53 AM
‎2008 May 16 7:56 AM
Hi,
if you have internal table table like this
aa bb cc n sum
xxx xxx xxx 2
yyy yyy yyy 3
zzz zzz zzz 4 9
to get the sum of sevaral data in the same line
Use this...
codedata: total_SUM type f.
SELECT SUM( sum ) INTO total_SUM
FROM itab
WHERE
aa = 'xxx' AND
bb = 'xxx'.[/code]
Check this link...
http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/select_c.htm
You can also check a simillar demo program in se38.
DEMO_EXTRACT_CNT_SUM.
Regards,
Raj.
‎2008 May 16 7:58 AM
Hi,
U can ur method to get the sum. But do remember to clear the <wa-field2> after sum.
Thanks
‎2008 May 16 8:00 AM
Hi,
If u want to add the values of work area u can use
Collect statement .
LOOP AT sflight_tab INTO sflight_wa.
AT NEW connid.
WRITE: / sflight_wa-carrid,
sflight_wa-connid.
ULINE.
ENDAT.
WRITE: / sflight_wa-fldate,
sflight_wa-seatsocc.
AT END OF connid.
SUM.
ULINE.
WRITE: / 'Sum',
sflight_wa-seatsocc UNDER sflight_wa-seatsocc.
SKIP.
ENDAT.
AT END OF carrid.
SUM.
ULINE.
WRITE: / 'Carrier Sum',
sflight_wa-seatsocc UNDER sflight_wa-seatsocc.
NEW-PAGE.
ENDAT.
AT LAST.
SUM.
WRITE: / 'Overall Sum',
sflight_wa-seatsocc UNDER sflight_wa-seatsocc.
ENDAT.
ENDLOOP.
This is just an example ........
‎2008 May 16 8:02 AM
You can use :
1. sum = add <wa>-field2 to sum.
2. sum = sum + <wa>-field2.
3. use the keyword "SUM". It is used in this way :
The statement SUM can only be specified within a loop starting with LOOP, and is only considered within a AT- ENDAT control structure. Prerequisites for using the statement SUM include using the addition INTO in the LOOP statement, and that the specified work area wa is compatible with the row type of the internal table. In addition, SUM cannot be used when the row type of the internal table itab contains components that are tables.
The statement SUM calculates the component total with the numeric data type (i, p, f) of all rows in the current control level and assigns these to the components of the work area wa. In the control levels FIRST, LAST, and outside of an AT-ENDAT control structure, the system calculates the sum of numeric components of all rows in the internal table.
Please see if this is helpful!!
‎2008 May 16 8:03 AM
Hi,
If you want to add only a particular field irrespective of the key fields in the table, your code is Ok. Incase you want add the fields based on certain key fields, just declare another internal table with same structure with key addition, mention the key fields. Now loop at the main internal table, assign the workarea to new one, use collect wa to internal table will sum all the numeric field in it and keep a single record for a key field.
ex.
types: begin of ty_xxx,
yyy type c,
zzz type c,
aaa type i,
end of ty_xxx.
data: i_tab type table of ty_xxx with key yyy zzz.
loop at itab into wa.
move wa to wa_new.
collect wa_new into i_tab.
endloop.
itab:
A B 10
A B 20
B C 30
B C 40
i_tab:
A B 30
B C 70
Regards,
Muthu.