Modern ABAP: Reduce# operator and FOR GROUPS

Estimated read time 3 min read

Business Requirement:

A customer wants to show the sum of GR posted material document & purchase orders(s) net price using the data updated in MSEG & EKPO tables.  For clear understanding breakdown of requirement here,

For each line item of PO should be processed like ERFME (qty) * NETPR (amt) and sum the total amount of line item. 

i.e., 1st line item- 5 * 10.00 = 50.00

      2nd line item- 3 * 100.00 = 300.00

TOTAL = 350.00 

Let’s implement this in classic ABAP LOOP…ENDLOOP:

SORT lt_tab BY MBLNR.
LOOP AT lt_tab INTO DATA(ls_data).
AT NEW mblnr.
CLEAR lv_netpr.
APPEND INITIAL LINE TO lt_final REFERENCE INTO DATA(lr_final).
lr_final->mblnr = ls_data-mblnr.
ENDAT.
lv_netpr = lv_netpr + ( ls_data-erfmg * ls_data-netpr ).
lr_final->netpr = lv_netpr.
ENDLOOP.
FREE lr_final.

The output as follows,

 

Let’s achieve this in Modern ABAP using with #FOR GROUPS and #Reduce# operator

lt_final = VALUE #( FOR GROUPS ls_group OF ls_data IN lt_tab
GROUP BY( mblnr = ls_data-mblnr )
LET lv_total = REDUCE netpr( INIT lv_sum = 0
FOR ls_row IN GROUP ls_group
NEXT lv_sum = lv_sum + ( ls_row-netpr * ls_row-erfmg ) )
IN
VALUE #( mblnr = ls_group-mblnr
netpr = lv_total ) ) ).

In debugger Mode,

The GROUP BY clause could be used in classic LOOP…ENDLOOP as well with the field symbol. Kindly refer the blogs that are available in internet.

Thanks for your time!

 

​ Business Requirement:A customer wants to show the sum of GR posted material document & purchase orders(s) net price using the data updated in MSEG & EKPO tables.  For clear understanding breakdown of requirement here,For each line item of PO should be processed like ERFME (qty) * NETPR (amt) and sum the total amount of line item. i.e., 1st line item- 5 * 10.00 = 50.00      2nd line item- 3 * 100.00 = 300.00TOTAL = 350.00 Let’s implement this in classic ABAP LOOP…ENDLOOP:SORT lt_tab BY MBLNR.
LOOP AT lt_tab INTO DATA(ls_data).
AT NEW mblnr.
CLEAR lv_netpr.
APPEND INITIAL LINE TO lt_final REFERENCE INTO DATA(lr_final).
lr_final->mblnr = ls_data-mblnr.
ENDAT.
lv_netpr = lv_netpr + ( ls_data-erfmg * ls_data-netpr ).
lr_final->netpr = lv_netpr.
ENDLOOP.
FREE lr_final.The output as follows, Let’s achieve this in Modern ABAP using with #FOR GROUPS and #Reduce# operatorlt_final = VALUE #( FOR GROUPS ls_group OF ls_data IN lt_tab
GROUP BY( mblnr = ls_data-mblnr )
LET lv_total = REDUCE netpr( INIT lv_sum = 0
FOR ls_row IN GROUP ls_group
NEXT lv_sum = lv_sum + ( ls_row-netpr * ls_row-erfmg ) )
IN
VALUE #( mblnr = ls_group-mblnr
netpr = lv_total ) ) ).In debugger Mode,The GROUP BY clause could be used in classic LOOP…ENDLOOP as well with the field symbol. Kindly refer the blogs that are available in internet.Thanks for your time!   Read More Technology Blog Posts by Members articles 

#SAP

#SAPTechnologyblog

You May Also Like

More From Author