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: 

How to compute sum of fields

former_member602116
Participant

Hi All,

I have a requirement where I should sum up all values 'GRMENGE'. I want to check if the total for 'GRMENGE' is equal to the value of 'MENGE'. How should I do this? have tried AT ENDAT but don't know how to use it based on my requirement.

Thank you.

Regards,

Katherine Darunday

1 ACCEPTED SOLUTION

DoanManhQuynh
Active Contributor
0 Kudos

your order of fields is: EBELN, EBELP then you dont have to do AT...2 times, just AT...EBELP is enough. Remember to sort the table first. something like:

LOOP AT itab INTO DATA(wa).
  wa_tmp = wa.
  lv_sum = lv_sum + wa-grmenge.
  AT END OF wa-ebelp.
    wa_tmp-grmenge = lv_sum.
    CLEAR lv_sump.
  ENDAT.
ENDLOOP.

You can also try statement SUM. look at demo program DEMO_INT_TABLES_AT_NESTED may help you understand it more.

btw, if you could try new way of ABAP, instead of AT...you could LOOP AT...GROUP BY...and REDUCE to sumarizing ( look at demo: DEMO_LOOP_GROUP_BY_AGGREGATES ).

6 REPLIES 6

ujjwal16
Participant
0 Kudos

If you have to sum all the values of 'GRMENGE' irrespective of 'EBELN' then use AT LAST and write it just before the END LOOP statement, as this run for last iteration of the loop.

And if the requirement is to sum 'GRMENGE' for each 'EBELN' then use AT NEW , as this act as a control break for group of data, so for each group of unique 'EBELN' you will be getting the total sum.

0 Kudos

Hi Kumar,

I actually need to sum all the values of 'GRMENGE' for each EBELN AND EBELP combination.

Can I do it like :

AT NEW EBELN.

AT NEW EBELP.

SUM.

ENDAT.

ENDAT.

?

Where does the sum gets stored in this?

Thank you.

ujjwal16
Participant

Follow this:

AT NEW EBELN EBELP.

SUM.

END AT.

Now, your question is where does SUM value get stored. Here as you are

adding 'GRMENGE' value so SUM will be stored in work area of 'GRMENGE'

as we use control break statements (AT FIRST, AT END, AT NEW, AT LAST)

inside the LOOP only. so here it might be gw_grmenge where gw_grmenge is

work area which hold value of 'GRMENGE' for each loop iteration.

NOTE: AT NEW statement will not work properly if you have a WHERE condition on your loop.

DoanManhQuynh
Active Contributor
0 Kudos

your order of fields is: EBELN, EBELP then you dont have to do AT...2 times, just AT...EBELP is enough. Remember to sort the table first. something like:

LOOP AT itab INTO DATA(wa).
  wa_tmp = wa.
  lv_sum = lv_sum + wa-grmenge.
  AT END OF wa-ebelp.
    wa_tmp-grmenge = lv_sum.
    CLEAR lv_sump.
  ENDAT.
ENDLOOP.

You can also try statement SUM. look at demo program DEMO_INT_TABLES_AT_NESTED may help you understand it more.

btw, if you could try new way of ABAP, instead of AT...you could LOOP AT...GROUP BY...and REDUCE to sumarizing ( look at demo: DEMO_LOOP_GROUP_BY_AGGREGATES ).

0 Kudos

Hi,

I followed your suggestion and it worked. Thanks a lot!

former_member184158
Active Contributor
0 Kudos

Hallo Katherine Darunday,

you can try with loop at group by better than at new, at end.

DATA menge_cnt TYPE i.
DATA grmenge_cnt TYPE i.
TYPES: BEGIN OF ts_cols,
         ebeln   TYPE ebeln,
         ebelp   TYPE ebelp,
         menge   TYPE bstmg,
         grmenge TYPE bstmg,
       END OF ts_cols.
TYPES tt_cols TYPE STANDARD TABLE OF ts_cols WITH DEFAULT KEY.
DATA(lt_tab) = VALUE tt_cols(
    ( ebeln = '4910145234 ' ebelp = '10' menge = 100 grmenge = 100 )
  ( ebeln = '4910145234'    ebelp = '10' menge = 100 grmenge = 150 )
  ( ebeln = '4910145234'    ebelp = '10' menge = 100 grmenge = 50 )


  ( ebeln = '4910145234' ebelp = '20' menge = 100 grmenge = 200 )
  ( ebeln = '4910145234' ebelp = '20' menge = 100 grmenge = 150 )
  ( ebeln = '4910145234' ebelp = '20' menge = 100 grmenge = 50 )
  ).
START-OF-SELECTION.
  LOOP AT lt_tab INTO DATA(ls_col)
  GROUP BY ( id1 = ls_col-ebeln
              id2 = ls_col-ebelp
             ).
    CLEAR: menge_cnt, grmenge_cnt.
    "ASCENDING REFERENCE INTO DATA(group_ref).
    LOOP AT GROUP ls_col INTO DATA(line).


      menge_cnt = menge_cnt + line-menge.
      grmenge_cnt = grmenge_cnt + line-grmenge.
    ENDLOOP.
    IF menge_cnt = grmenge_cnt.
      WRITE:/ ls_col-ebeln, ls_col-ebelp, 'are same' , 'menge_cnt = '  , menge_cnt, 'grmenge_cnt = '  , grmenge_cnt .
    ELSE.
      WRITE:/ ls_col-ebeln, ls_col-ebelp, 'are NOT same' , 'menge_cnt = '  , menge_cnt, 'grmenge_cnt = '  , grmenge_cnt .
    ENDIF.
  ENDLOOP.

There is also one question about loop group by, you can refer to this post.

Best regards

Ebrahim