Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
douglas_linkup
Explorer
Calm! Don't hit me. I exaggerated in the title, but it isn't at all a lie. I don't want to stay repeating horst.keller, but some ABAPers still come to me: "I never had to use REDUCE..."

An expression with the REDUCE reduction operator creates a result of a specified data type using the type of one or more condition expressions.
A new and more performative way to not use LOOP AT NEW, for example ( ok ok .. is very old ).
With REDUCE it is possible to do a mathematical operation grouping by the items of a certain table for example. That is, instead of making a LOOP inside LOOP between two tables, you can use REDUCE to directly access the items you need to read. The reading is based on a table, so you can use the FILTER operator, for example, to generate the internal table with only the desired items. See the example below:

 
REPORT ysamplelkp_reduce.

*------------------------------------------------*
* PURPOSE: For each order item, calculate
* the total value from the KOMV table
* (Conditions) where:
* Condition PBXX = value X
* Condition RA01 = value Y
*
* TOTAL / ITEM (NETWR) = Sum PBXX + Sum RA01
*------------------------------------------------*

CLASS demo DEFINITION.
PUBLIC SECTION.
CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.
METHOD main.

TYPES:
BEGIN OF y_ekpo,
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
netwr TYPE ekpo-netwr,
END OF y_ekpo,
yt_ekpo TYPE SORTED TABLE OF y_ekpo
WITH UNIQUE KEY ebeln ebelp,

BEGIN OF y_komv,
knumv TYPE komv-knumv,
kposn TYPE komv-kposn,
kschl TYPE komv-kschl,
kwert TYPE komv-kwert,
END OF y_komv,
yt_komv TYPE SORTED TABLE OF y_komv
WITH NON-UNIQUE KEY knumv kposn kschl
WITH NON-UNIQUE SORTED KEY key_kposn COMPONENTS kposn kschl.

DATA it_ekpo TYPE yt_ekpo.
DATA it_komv TYPE yt_komv.

it_ekpo =
VALUE #(
( ebeln = '0040000000' ebelp = '10' )
( ebeln = '0040000000' ebelp = '20' )
( ebeln = '0040000000' ebelp = '30' )
).

it_komv =
VALUE #(
( knumv = '0000000001' kposn = '10' kschl = 'RA01' kwert = '10.00' )
( knumv = '0000000001' kposn = '10' kschl = 'PBXX' kwert = '350.00' )
( knumv = '0000000001' kposn = '20' kschl = 'RA01' kwert = '2.00' )
( knumv = '0000000001' kposn = '20' kschl = 'RA01' kwert = '3.50' )
( knumv = '0000000001' kposn = '20' kschl = 'PBXX' kwert = '400.00' )
( knumv = '0000000001' kposn = '10' kschl = 'RA01' kwert = '5.00' )
( knumv = '0000000001' kposn = '10' kschl = 'PBXX' kwert = '200.00' )
).
DATA(out) = cl_demo_output=>new( )->write_data( it_ekpo ).

out->write_data( it_komv ).

*------------------------------------------------*
* Using LOOP and Work area (on purpose)
*------------------------------------------------*
DATA st_ekpo LIKE LINE OF it_ekpo.
DATA st_ekpox LIKE LINE OF it_ekpo.
DATA st_komv LIKE LINE OF it_komv.

LOOP AT it_ekpo
INTO st_ekpo.

st_ekpox = st_ekpo.

AT NEW ebelp.

LOOP AT it_komv
INTO st_komv
USING KEY key_kposn
WHERE kposn EQ st_ekpox-ebelp.

st_ekpo-netwr = st_ekpo-netwr + st_komv-kwert.

ENDLOOP.

MODIFY it_ekpo FROM st_ekpo TRANSPORTING netwr.

ENDAT.

ENDLOOP.

out->write_text( 'Using LOOP and Work area:' ).

out->write_data( it_ekpo ).

*------------------------------------------------*
* Using REDUCE ( It's beautiful! )
*------------------------------------------------*
LOOP AT it_ekpo
ASSIGNING FIELD-SYMBOL(<fs_ekpo>).
<fs_ekpo>-netwr = REDUCE netwr( INIT val TYPE netwr
FOR wa IN
FILTER #( it_komv
USING KEY key_kposn
WHERE kposn EQ CONV #( <fs_ekpo>-ebelp ) )
NEXT val = val + wa-kwert ).
ENDLOOP.

out->write_text( 'Using REDUCE:' ).

out->write_data( it_ekpo )->display( ).


ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
demo=>main( ).

Giving...





REDUCE:



 

This is a simple example. Probably I can get the same result without using LOOP in any way ( it's for you to think ). For me, it's a very clean code. For more details, I recommend a read on at Help SAP for Iteration Expressions.

 

Hugs!
13 Comments
Labels in this area