09-19-2018 9:00 PM
TYPES: BEGIN OF ty_ekpo,
werks TYPE werks_d,
menge TYPE bstmg,
END OF ty_ekpo,
tt_ekpo TYPE TABLE OF ty_ekpo WITH DEFAULT KEY.
DATA: lt_werks TYPE tt_ekpo.
DATA(lt_ekpo) = VALUE tt_ekpo(
( werks = 1000 menge = CONV bstmg( '100.50' ) )
( werks = 1000 menge = CONV bstmg( 100 ) )
( werks = 1001 menge = CONV bstmg( '100.60' ) )
( werks = 1001 menge = CONV bstmg( 100 ) )
( werks = 1002 menge = CONV bstmg( '100.70' ) )
( werks = 1002 menge = CONV bstmg( 100 ) ) ).
BREAK-POINT.
LOOP AT lt_ekpo ASSIGNING FIELD-SYMBOL(<fs>)
GROUP BY ( werks = <fs>-werks )
ASCENDING
ASSIGNING FIELD-SYMBOL(<grp>).
** Two Statements
* DATA(lt_werks1) = VALUE tt_ekpo( FOR m IN GROUP <grp> ( m ) ).
* lw_werks = REDUCE #( INIT r_wa TYPE ty_ekpo
* FOR wa IN lt_werks1
* NEXT r_wa-menge = r_wa-menge + wa-menge
* r_wa-werks = wa-werks ).
* APPEND lw_werks TO lt_werks.
* Reduce
DATA(lw_total) = REDUCE ty_ekpo(
INIT r_total TYPE ty_ekpo
FOR wa IN VALUE tt_ekpo( FOR m IN GROUP <grp> ( m ) )
LET x = wa IN
NEXT r_total-menge = wa-menge + r_total-menge
r_total-werks = wa-werks ).
APPEND lw_total TO lt_werks.
ENDLOOP.
cl_demo_output=>display( lt_werks ).
In the above example, i am trying to add Quantities for each plant. It is working fine with the REDUCE Operator how ever i'm forced to use LET expression. (There is no use with variable 'x' )
Why i added LET: When i debug the REDUCE code, after "FOR m IN GROUP <grp>" statement, the cursor is going to the "NEXT r_total-menge" statement, just to prevent that i have added LET statement.
Can't we achieve this with out LET. Please correct me.
Thanks
09-20-2018 2:24 AM
First I dont understand your reason to add LET statement because with no LET it still sum data correctly and have same output as you do with two statements part.
Second, I think you dont have to using FOR 2 times, it could be simple like this:
DATA(LW_TOTAL) = REDUCE TY_EKPO(
INIT R_TOTAL TYPE TY_EKPO
FOR M IN GROUP <GRP>
NEXT R_TOTAL-MENGE = M-MENGE + R_TOTAL-MENGE
R_TOTAL-WERKS = M-WERKS ).
APPEND LW_TOTAL TO LT_WERKS.
09-20-2018 2:24 AM
First I dont understand your reason to add LET statement because with no LET it still sum data correctly and have same output as you do with two statements part.
Second, I think you dont have to using FOR 2 times, it could be simple like this:
DATA(LW_TOTAL) = REDUCE TY_EKPO(
INIT R_TOTAL TYPE TY_EKPO
FOR M IN GROUP <GRP>
NEXT R_TOTAL-MENGE = M-MENGE + R_TOTAL-MENGE
R_TOTAL-WERKS = M-WERKS ).
APPEND LW_TOTAL TO LT_WERKS.
09-20-2018 1:41 PM