2007 Mar 30 6:40 AM
Hello,
I am maintaining a code as given below.
LOOP AT itab INTO totwa.
SUM.
ENDLOOP.
totwa is defined as a workarea which is needed to hold the sum totals of the fields in the itab. According to the documentation of SUM, it should calculate the sum totals and should put the totals in the workarea mentioned. But somehow its not calculating the value. Could anyone please help me in resolving this issue. Please guide me with your thoughts as to what might have gone wrong.
Note: the itab fields are of type P.
Thanks in advance
Sudha Naik
2007 Mar 30 6:54 AM
Hi..,
check this code ... its working..
DATA: BEGIN OF LINE,
COL1 TYPE C,
COL2 TYPE p decimals 2,
COL3 TYPE I,
END OF LINE.
DATA ITAB LIKE HASHED TABLE OF LINE
WITH UNIQUE KEY COL1 COL2.
LINE-COL1 = 'A'.
DO 3 TIMES.
LINE-COL2 = SY-INDEX.
LINE-COL3 = SY-INDEX ** 2.
INSERT LINE INTO TABLE ITAB.
ENDDO.
LINE-COL1 = 'B'.
DO 3 TIMES.
LINE-COL2 = 2 * SY-INDEX.
LINE-COL3 = ( 2 * SY-INDEX ) ** 2.
INSERT LINE INTO TABLE ITAB.
ENDDO.
SORT ITAB.
LOOP AT ITAB INTO LINE.
WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.
SUM.
endloop.
WRITE: / LINE-COL2, LINE-COL3.
<b>*** U might be clearing the Work area after the SUM statement .. check that !! even after the endloop also u should not clear the work area till u use those summed values.</b>
Hope this solves ur problem !!
regards,
sai ramesh
2007 Mar 30 6:54 AM
Hi..,
check this code ... its working..
DATA: BEGIN OF LINE,
COL1 TYPE C,
COL2 TYPE p decimals 2,
COL3 TYPE I,
END OF LINE.
DATA ITAB LIKE HASHED TABLE OF LINE
WITH UNIQUE KEY COL1 COL2.
LINE-COL1 = 'A'.
DO 3 TIMES.
LINE-COL2 = SY-INDEX.
LINE-COL3 = SY-INDEX ** 2.
INSERT LINE INTO TABLE ITAB.
ENDDO.
LINE-COL1 = 'B'.
DO 3 TIMES.
LINE-COL2 = 2 * SY-INDEX.
LINE-COL3 = ( 2 * SY-INDEX ) ** 2.
INSERT LINE INTO TABLE ITAB.
ENDDO.
SORT ITAB.
LOOP AT ITAB INTO LINE.
WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.
SUM.
endloop.
WRITE: / LINE-COL2, LINE-COL3.
<b>*** U might be clearing the Work area after the SUM statement .. check that !! even after the endloop also u should not clear the work area till u use those summed values.</b>
Hope this solves ur problem !!
regards,
sai ramesh
2007 Mar 30 7:00 AM
Hello Suha
According to the ABAP documentation the SUM statement is used in a specific situation:
<b>Syntax
SUM. </b>
<b>Effect </b>
<i>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. </i>
<i>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.</i>
I hope the following sample report will clarify the use of <b>SUM </b>and <b>COLLECT</b>.
*&---------------------------------------------------------------------*
*& Report ZUS_SDN_COLLECT
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zus_sdn_collect.
TYPES: BEGIN OF ty_s_line.
TYPES: key(1) TYPE n.
TYPES: value TYPE p DECIMALS 2.
TYPES: END OF ty_s_line.
TYPES: ty_t_itab TYPE STANDARD TABLE OF ty_s_line
WITH DEFAULT KEY.
DATA:
gs_line TYPE ty_s_line,
gt_itab TYPE ty_t_itab,
gt_itab_coll TYPE ty_t_itab.
START-OF-SELECTION.
DO 3 TIMES.
gs_line-key = syst-index.
gs_line-value = syst-index * '2.3'.
APPEND gs_line TO gt_itab.
APPEND gs_line TO gt_itab.
ENDDO.
gs_line-key = 4.
gs_line-value = '5.5'.
APPEND gs_line TO gt_itab.
WRITE: / 'Initial list'.
LOOP AT gt_itab INTO gs_line.
WRITE: / gs_line-key,
gs_line-value.
ENDLOOP.
WRITE: / syst-uline.
SKIP 2.
WRITE: / 'Using SUM statement with AT END OF'.
SORT gt_itab BY key.
LOOP AT gt_itab INTO gs_line.
AT END OF key.
SUM.
WRITE: / gs_line-key,
gs_line-value.
ENDAT.
ENDLOOP.
WRITE: / syst-uline.
SKIP 2.
WRITE: / 'Using SUM statement without control structure (1)'.
SORT gt_itab BY key.
LOOP AT gt_itab INTO gs_line.
SUM.
WRITE: / gs_line-key,
gs_line-value.
ENDLOOP.
WRITE: / syst-uline.
SKIP 2.
WRITE: / 'Using SUM statement without control structure (2)'.
SORT gt_itab BY key.
LOOP AT gt_itab INTO gs_line.
SUM.
WRITE: / gs_line-key,
gs_line-value.
EXIT.
ENDLOOP.
WRITE: / syst-uline.
SKIP 2.
WRITE: / 'Using COLLECT statement'.
REFRESH: gt_itab_coll.
LOOP AT gt_itab INTO gs_line.
COLLECT gs_line INTO gt_itab_coll.
ENDLOOP.
LOOP AT gt_itab_coll INTO gs_line.
WRITE: / gs_line-key,
gs_line-value.
ENDLOOP.
WRITE: / syst-uline.
SKIP 2.
WRITE: / 'Using COLLECT statement for total sum'.
REFRESH: gt_itab_coll.
LOOP AT gt_itab INTO gs_line.
gs_line-key = 0.
COLLECT gs_line INTO gt_itab_coll.
ENDLOOP.
LOOP AT gt_itab_coll INTO gs_line.
WRITE: / gs_line-key,
gs_line-value.
ENDLOOP.
WRITE: / syst-uline.
SKIP 2.
END-OF-SELECTION.
Regards
Uwe
2007 Mar 30 7:08 AM
Hi,
<b>LOOP AT itab INTO totwa.
SUM.
ENDLOOP.</b>
The above statments should work, check it in the debugging, workarea totwa will have the SUM values in it. Do not clear the work area before moving the data into the Internal table
Regards
Sudheer
2007 Mar 30 7:08 AM
Thanks for the fast response.
I am not clearing the itab. The information in the itab is fetched from the screen. During one scnerio ( update of an already existing record), this code is working fine and during the other ( insert, creating a new entry) , this is not working. I am not able to understand why it is so. While debugging, I am not able to find any difference in the itab in both the cases.
2007 Mar 30 12:13 PM
Hi,
Better use control break statements, see the below Ex how to use control breaks for summing:
DATA:BEGIN OF ITAB OCCURS 0,
A1(10) TYPE C,
A2 TYPE I,
END OF ITAB.
DATA:TOT TYPE I.
ITAB-A1 = 'char1'.
ITAB-A2 = '10'.
APPEND ITAB.
ITAB-A1 = 'char1'.
ITAB-A2 = '20'.
APPEND ITAB.
ITAB-A1 = 'char1'.
ITAB-A2 = '30'.
APPEND ITAB.
ITAB-A1 = 'char2'.
ITAB-A2 = '10'.
APPEND ITAB.
ITAB-A1 = 'char2'.
ITAB-A2 = '20'.
APPEND ITAB.
SORT ITAB BY A1.
loop at itab.
read table itab index sy-index.
at new a1.
clear tot.
write:/ itab-a1.
endat.
tot = tot + itab-a2.
at end of a1.
write: 10 tot.
endat.
endloop.
Hope it helps,
Regards,