I have mentioned the Reduce command in another post, we exemplify it with different uses below,
Let’s give the code first,
* Number of values with ‘XYZ’ criterion in a table
DATA(lv_lines) = REDUCE i( INIT x = 0 FOR wa IN gt_itab
WHERE( F1 = ‘XYZ’ ) NEXT x = x + 1 ).
* Sum of values
DATA(lv_sum) = REDUCE i( INIT x = 0 FOR wa IN itab NEXT x = x + wa ).
Example usage, we have a MARA table, I pull the values in it
SELECT matnr, mtart FROM mara INTO TABLE @DATA(lt_mara).
We got 934 results, I want the number of material type finished and semi-finished products from here,
I want the number of semi-finished products,
Result:
Let’s see if it counted correctly
In our other example, I will take the EWMS4-02 available stock from the MARD table to the internal table and calculate the total amount (you can do it with SUM only with SQL command, but this is not relevant to our example right now 😊)
We did our SELECT, 10 results came, by the way, let’s calculate the total, 1795 units returned in total, let’s see how many REDUCE returns.
We write our code
There is an important point here, the value I boxed in red is used to determine the type of the new variable that will occur when using the inline declaration. If you are going to put it in a previously created variable, you can type a # sign, it will get its type directly from there, we will create it from scratch as DATA (lv_total), so we have to give it a reference type.
Result:
As we saw above, it found 1,795 .