07-20-2022 2:12 AM
How to do summation in the internal table with condition?
For example, my itab have the following data:
itab l_bcode_i :
vgpos[1] :10 , menge[1]:600
vgpos[2] :10 , menge[2]:500
vgpos[3] :20 , menge[3]:400
vgpos[4] :20 , menge[4]:300
vgpos[5] :30 , menge[5]:200
vgpos[6] :30 , menge[6]:10
the requirement request to sum up the data with the same vgpos number by using the menge, and use to display another message.
I try to show
vgpos: 10 menge = 1100
vgpos:20 menge = 500
vgpos:30 menge = 210
I try to use loop and sum, however the result not as the result I need because it will total up all the menge value by ignored the where clause condition that I set. Any recommendation or suggestion is appreciated:
at last it will should menge = 1810
LOOP AT LT_BCODE_I INTO LT_BCODE_I WHERE VGPOS = LW_LIPS2-VGPOS.
AT LAST.
SUM.
cl_demo_output=>display( LT_BCODE_I-MENGE ).
ENDAT.
ENDLOOP.
07-20-2022 6:09 AM
Hi
You should use AT END OF LT_LIPS-VGBEL instead of AT LAST.
LOOP AT LT_BCODE_I INTO LT_BCODE_I WHERE VGBEL = LT_LIPS-VGBEL AND VGPOS = LT_LIPS-VGPOS.
AT END OF LT_LIPS-VGBEL
SUM.
cl_demo_output=>display( LT_BCODE_I-MENGE ).
ENDAT.
ENDLOOP.
Regards,
Venkat
07-20-2022 6:09 AM
Hi
You should use AT END OF LT_LIPS-VGBEL instead of AT LAST.
LOOP AT LT_BCODE_I INTO LT_BCODE_I WHERE VGBEL = LT_LIPS-VGBEL AND VGPOS = LT_LIPS-VGPOS.
AT END OF LT_LIPS-VGBEL
SUM.
cl_demo_output=>display( LT_BCODE_I-MENGE ).
ENDAT.
ENDLOOP.
Regards,
Venkat
07-20-2022 7:04 AM
07-20-2022 7:22 AM
I recommend to avoid "SUM" because it's something completely counter-intuitive in the world of programming languages.
Why not just using X = X + value?
07-20-2022 10:18 AM
07-20-2022 10:23 AM
if use X = X + value, how to applied condition? any recommendation as I mention in the question... only need to sum up the data that with the same vgpos, all data in same internal table, any recommendation? I still new to abap, will appreciated with all the suggestion given. Thank you
07-20-2022 3:21 PM
You'll probably say I'm crazy 😉
TYPES: BEGIN OF ty_vgbel_sum,
vgbel TYPE lips-vgbel,
sum TYPE lips-menge,
END OF ty_vgbel_sum,
ty_vgbel_sum_table TYPE STANDARD TABLE OF ty_vgbel_sum WITH EMPTY KEY.
DATA(sum) = VALUE ty_vgbel_sum_table(
FOR GROUPS <group_ebeln> OF <line> IN LT_BCODE_I
WHERE ( VGBEL = LT_LIPS-VGBEL AND VGPOS = LT_LIPS-VGPOS )
GROUP BY ( ebeln = <line>-ebeln )
( vgbel = <line>-vgbel
sum = REDUCE decfloat34(
INIT sum TYPE decfloat34
FOR <line_2> IN GROUP <group_ebeln>
NEXT sum = sum + <line_2>-menge ) ) ).