SAP ABAP Reduce Operator example for sum

 REDUCE – Reduction Operator 

In this example, we will see, how to use reduce operator, instead of loop inside loop for doing the sum of a field.

 Scenario:

·         Select, Customer ID and Name from KNA1 table along with this add one additional column which will hold value of payment from FI table.

·         Select sum of field DMBTR from BSID table for the all the customer based on first selection.

·         Use Reduce operator for performing of sum operation.

REPORT ZKT_REDUCE_OPERATOR.

data : gv_kunnr type bsid-kunnr.

SELECTION-SCREEN begin of BLOCK b1 WITH FRAME TITLE text-001.
 select-OPTIONS : s_kunnr for gv_kunnr OBLIGATORY.
SELECTION-SCREEN end of BLOCK b1.


START-OF-SELECTION.
     perform get_data.


end-of-SELECTION.
*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form get_data .

    select kunnr, name1, cast( 0 as DEC ) as Amount from kna1 into TABLE @data(gt_kna1)
    where kunnr in @s_kunnr.

   if gt_kna1[] is NOT INITIAL.

   select * from bsid into TABLE @data(gt_bsid) FOR ALL ENTRIES IN @gt_kna1
   WHERE kunnr = @gt_kna1-kunnr
     and BLART = 'BR'.


    loop at gt_kna1 ASSIGNING FIELD-SYMBOL(<fs1>).

    <fs1>-amount = REDUCE i( INIT i type dmbtr for wa_bsid in gt_bsid WHERE ( kunnr = <fs1>-kunnr )
                                  NEXT i = i + wa_bsid-dmbtr ).

    endloop.

   endif.

       cl_demo_output=>display( gt_kna1 ).

endform.

  • Selection Screen

Output 



Post a Comment

0 Comments

Total Pageviews