Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Calculate Sum based on condition

suityan98
Explorer
0 Kudos

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.
1 ACCEPTED SOLUTION

venkateswaran_k
Active Contributor

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

6 REPLIES 6

venkateswaran_k
Active Contributor

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

0 Kudos

Thank you, the problem fixed.

Sandra_Rossi
Active Contributor
0 Kudos

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?

0 Kudos

hmmm. any coding suggestion? because i only know this way to do

0 Kudos

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

0 Kudos

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 ) ) ).