‎2009 Jul 07 12:55 PM
Hi everybody ,
I want to sum in my itab values
For example My alv
Kunnr value1 total_value
A 5 0
A 4 9 " total field
B 2 0
B 2 0
B 3 7 " Total field
C 1 0
C 1 2 " Total field
My code is :
DATA : lv_total like itab-hedef1 .
SORT itab BY kunnr .
LOOP AT itab INTO gs_itab .
AT NEW kunnr .
lv_total = lv_total + itab-hedef1 .
gs_itab-hedeft = 0 .
ENDAT .
gs_itab-hedeft = lv_total .
MODIFY itab from gs_itab .
ENDLOOP.
I want to sum last of field . b
thanks reply
Edited by: Serkan Taskan on Jul 7, 2009 1:55 PM
‎2009 Jul 07 1:26 PM
Hi,
Please go through the entire code, you will get the logic.
data: begin of itab occurs 0,
name(4),
value(4),
total(4),
end of itab,
wa like line of itab,
v_value(4).
itab-name = 'A'.itab-value = '5'.
append itab.
itab-name = 'A'.itab-value = '4'.
append itab.
itab-name = 'B'.itab-value = '2'.
append itab.
itab-name = 'B'.itab-value = '2'.
append itab.
itab-name = 'B'.itab-value = '3'.
append itab.
itab-name = 'C'.itab-value = '1'.
append itab.
itab-name = 'C'.itab-value = '1'.
append itab.
loop at itab.
wa = itab. " Note moved to seperate work area because of AT control break use
v_value = v_value + wa-value.
at end of name.
wa-total = v_value.
modify itab from wa TRANSPORTING total.
clear v_value.
endat.
endloop.
loop at itab.
write: / itab-name,itab-value,itab-total.
endloop.
Hope this will help you.
Regards,
Smart Varghese
‎2009 Jul 07 12:59 PM
Hi,
Instead of using At New, use At End of and before using this statement make sure you are sorting the internal table and also see that the first field in your internal table is the one which you are using in this statement.
Regards,
Vinod.
‎2009 Jul 07 1:01 PM
DATA : lv_total like itab-hedef1 .
SORT itab BY kunnr .
LOOP AT itab INTO gs_itab .
AT NEW kunnr .
lv_total = lv_total + itab-hedef1 .
gs_itab-hedeft = 0 .
ENDAT .
gs_itab-hedeft = lv_total .
MODIFY itab from gs_itab .
ENDLOOP.
use on change-of kunnr instead of at-new.
use keyword SUM for addition inside this.
‎2009 Jul 07 1:01 PM
Hi
DATA : lv_total like itab-hedef1 .
SORT itab BY kunnr .
LOOP AT itab INTO gs_itab .
AT NEW kunnr .
SUM .
MODIFY itab from gs_itab .
ENDLOOP.
Instead of summing manually,use the keyword SUM,it can give u the required results.
Regards,
Sreeram
‎2009 Jul 07 1:04 PM
Hi
The control will go inside AT NEW if the KUNNR is new. So in the above code it will not sum as only once it will go inside the summing statement for each KUNNR. Instead you can use AT END OF KUNNR. Try this code
Loop at i_tab into wa_tab.
wa_tab-total = wa_tab-total + wa_tab-value.
AT END OF KUNNR.
wa_tab-value = wa_tab-total.
clear : wa_tab-total.
END AT.
MODIFY i_tab FROM wa_tab
TRANSPORTING value
WHERE kunnr = wa_tab-kunnr.
Endloop.
‎2009 Jul 07 1:06 PM
Hi,
The COLLECT key word helps to achive the required result.
Use the same.
Regards,
Ankur Parab
‎2009 Jul 07 1:08 PM
Hi,
You can use AT END OF Statement of field KUNNR inside
the loop on your internal table.
Calculate the sum of your total_value field and
Modify your internal table with the changed
TOTAL_VALUE field value using TRANSPORTING that field name.
Hope it helps
Regards
Mansi
‎2009 Jul 07 1:30 PM
I add this code but end of data clear data .. so my data cleared . look at example 6000 is *****
My code is
LOOP AT itab INTO gs_itab.
lv_total = lv_total + gs_itab-hedef1.
gs_itab-hedeft = lv_total.
AT END OF kunnr.
MODIFY itab FROM gs_itab .
CLEAR : lv_total.
ENDAT .
ENDLOOP.
Before Code
field1 total
6.000 0
6.000 0
6,000 0
After Code
field1 total
6.000 0
6.000 0
0 18.000
I want to this
field1 total
6.000 0
6.000 0
6.000 18.000
Edited by: Serkan Taskan on Jul 7, 2009 2:31 PM
‎2009 Jul 07 1:40 PM
Hi
Please note within AT END....ENDAT, the contents of the work area which is of type CHAR will be *****
So in your code instead of modifying the the entire fields of the work area use the TRANSPORTING parameter for your MODIFY statement
LOOP AT itab INTO gs_itab.
lv_total = lv_total + gs_itab-hedef1.
gs_itab-hedeft = lv_total.
AT END OF kunnr.
MODIFY itab FROM gs_itab TRANSPORTING hedeft .
CLEAR : lv_total.
ENDAT .
ENDLOOP.
Once you are out of the ENDAT statement you will get all the contents of your workarea back.
Thanks
Winnie
‎2009 Jul 07 1:14 PM
SORT itab BY kunnr .
LOOP AT itab INTO gs_itab .
AT NEW kunnr .
gs_itab-hedeft = 0 .
ENDAT .
lv_total = lv_total + itab-hedef1 .
at end of kunnr.
gs_itab-hedeft = lv_total .
MODIFY itab from gs_itab .
endat.
ENDLOOP.
‎2009 Jul 07 1:26 PM
Hi,
Please go through the entire code, you will get the logic.
data: begin of itab occurs 0,
name(4),
value(4),
total(4),
end of itab,
wa like line of itab,
v_value(4).
itab-name = 'A'.itab-value = '5'.
append itab.
itab-name = 'A'.itab-value = '4'.
append itab.
itab-name = 'B'.itab-value = '2'.
append itab.
itab-name = 'B'.itab-value = '2'.
append itab.
itab-name = 'B'.itab-value = '3'.
append itab.
itab-name = 'C'.itab-value = '1'.
append itab.
itab-name = 'C'.itab-value = '1'.
append itab.
loop at itab.
wa = itab. " Note moved to seperate work area because of AT control break use
v_value = v_value + wa-value.
at end of name.
wa-total = v_value.
modify itab from wa TRANSPORTING total.
clear v_value.
endat.
endloop.
loop at itab.
write: / itab-name,itab-value,itab-total.
endloop.
Hope this will help you.
Regards,
Smart Varghese