REDUCE
This is used to create a result of a data type specified using type from one or more iteration expressions.
As the name suggest , it is used generally to reduce sets of data objects to a single data object.
FILTER : Optional to use -
Used to filter out the values
Firstly lets see a very simple example to sum the column values in a table
TYPES : BEGIN OF ty_int1,
name TYPE char10,
value TYPE i,
value1 TYPE i,
END OF ty_int1,
tt_int1 TYPE sorted TABLE OF ty_int1 WITH non-UNIQUE KEY name.
DATA(lt_int1) = VALUE tt_int1(
( name = 'ENTRY1' value = '10' value1 = '20' )
( name = 'ENTRY2' value = '30' value1 = '40' )
( name = 'ENTRY1' value = '50' value1 = '60' )
( name = 'ENTRY2' value = '70' value1 = '80' )
( name = 'ENTRY2' value = '90' value1 = '100' )
).
Created table :
3.7.1. Get sum of a column
*Get sum of column 'VALUE'
DATA(lv_value_sum) = REDUCE i( INIT lv1 = 0
FOR ls_int1 in lt_int1
NEXT lv1 = lv1 + ls_int1-value
).
*Get sum of column 'VALUE1'
DATA(lv_value1_sum) = REDUCE i( INIT lv2 = 0
FOR ls_int1 in lt_int1
NEXT lv2 = lv2 + ls_int1-value1
).
Resultant values :
3.7.2. Using WHERE
*Get sum of rows having name = 'ENTRY1' .
DATA(ls_entry1_sum) = REDUCE ty_int1(
INIT ls_temp TYPE ty_int1
FOR ls_int1 in lt_int1
WHERE ( name = 'ENTRY1' )
NEXT ls_temp-value = ls_temp-value + ls_int1-value
ls_temp-value1 = ls_temp-value1 + ls_int1-value1
ls_temp-name = ls_int1-name
).
Resultant sum :
* Achieving same as above using filter
DATA : lv_name TYPE char10 VALUE 'ENTRY1'.
DATA(ls_entry2_sum) = REDUCE ty_int1(
INIT ls_temp TYPE ty_int1
FOR ls_int1 in
FILTER #( lt_int1 WHERE name = lv_name )
NEXT ls_temp-value = ls_temp-value + ls_int1-value
ls_temp-value1 = ls_temp-value1 + ls_int1-value1
ls_temp-name = ls_int1-name
).
Resultant Sum :
3.7.3. Use of LET
DATA(ls_using_let) = REDUCE i(
Next - Meshes
No comments:
Post a Comment