1
votes

I am stuck at a problem that I want to solve with REDUCE syntax.

I have this this internal table Purchase Orders

I need to sum the values of the column "quantity" and update column called "Totals quantity"

And the same thing with column price in column "Total Price". This is for each Purchase order.

I have this code right now

    Loop at it_numpos Into Data(ls_numpos).
     lv_valort = lv_valort + ls_numpos-netpr. " Purchase Order Total Price 
     lv_cantt  = lv_cantt  + ls_numpos-menge. " Purchase Total Quantity
    At end of ebeln.
     ls_numpos-zmenge3 = lv_cantt.
     ls_numpos-znetpr6 = lv_valort.
     Modify it_numpos From ls_numpos Transporting zmenge3 znetpr6 Where ebeln = ls_numpos-ebeln.
     Clear:  lv_cantt, ls_numpos, lv_valort.
    Endat.
EndLoop.

It is possible to transform this code to abap new syntax?

1
And also, please provide the minimal reproducible code, the actual result and the expected result. Nobody would spend time in proposing a solution with your input data typed manually. - Sandra Rossi
I edited the question.. Thanks for your response - Ivan Diaz Salas

1 Answers

1
votes

I don't think REDUCE is the right tool for the job here, as it is meant to reduce the values of a table to one single value. In your case there is not one single value, as you're calculating new values for each sales order. So you would need to somehow loop over the table grouping the items together, then use reduce, then loop again to assign the values back into the table. That would rather complicate the code and just for the sake of using new synax is probably not worth the trouble. I think a LOOP AT is the better choice here, though I'd use a LOOP AT ... GROUP BY and then two LOOP AT GROUP loops, which makes the whole processing quite readable:

LOOP AT order_items ASSIGNING FIELD-SYMBOL(<order_item>) GROUP BY <order_item>-id INTO DATA(order).
  DATA(total_price) = 0.

  LOOP AT GROUP order ASSIGNING <order_item>.
    total_price = total_price + <order_item>-price.
  ENDLOOP.

  LOOP AT GROUP order ASSIGNING <order_item>.
    <order_item>-total_price = total_price.
  ENDLOOP.
ENDLOOP.

However whether that is better than group level processing is up to you.