05-17-2019 5:39 AM
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
05-20-2019 2:51 AM
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 ).
05-17-2019 8:10 AM
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.
05-17-2019 10:25 AM
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.
05-17-2019 11:57 AM
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.
05-20-2019 2:51 AM
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 ).
05-21-2019 9:23 AM
05-20-2019 7:38 AM
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