3.5. Manipulation : Loop with GROUP BY

Loops with GROUP BY

This comes very handy when we want to group columns based on a particular column value.
Previously we use to do this using control break statements like AT START or AT END OF. 
 
Also with this we can dynamically store the number of element in a group using GROUP SIZE and index of group using GROUP INDEX. Using 'WITHOUT MEMBERS' we can create group if we don't want to access the elements of the groups, by default it is implemented as 'WITH MEMBERS' .
Lets implement the same in our ABAP code.

We will be using the same structure that is created before and adding the required values.

TYPES :  BEGIN OF ty_target,
                 name TYPE char10,
                 value TYPE char20,
                 exp   TYPE i,
                 END OF ty_target,
                 tt_target TYPE STANDARD TABLE OF  ty_target WITH DEFAULT KEY.

DATA(lt_target) = VALUE tt_target(
                                  ( name = 'TECH2' value = 'SAP' exp = '5' )
                                  ( name = 'DOMAIN2' value = 'ABAP2' exp = '10')
                                  ( name = 'TECH' value = 'SAP'  exp = '20')
                                  ( name = 'TECH3' value = 'SAP' exp = '15' )
                                  ( name = 'DOMAIN3' value = 'ABAP3' exp = '10')
                                  ).
Table Entries :

3..5.1. Get sum of experience based on the 'value' column

 
DATA : ls_temp TYPE ty_target,
             lt_temp TYPE STANDARD TABLE OF ty_target.


LOOP AT lt_target ASSIGNING FIELD-SYMBOL(<fs1>)
                          GROUP BY ( value = <fs1>-value
                                     size = GROUP SIZE         " Defines the number of rows in a group
                                     index = GROUP INDEX   " Index of group
                                    )
                          ASCENDING                  "Optional - by default its ascending , groups can be sorted
                         ASSIGNING FIELD-SYMBOL(<fs_grp>).

LOOP AT GROUP <fs_grp> ASSIGNING FIELD-SYMBOL(<fs2>).
   ls_temp-exp = ls_temp-exp + <fs2>-exp.  
ENDLOOP.

ls_temp-value = <fs2>-value.
APPEND ls_temp TO lt_temp.
CLEAR ls_temp.
ENDLOOP.

 Create table LT_TEMP  having sum of exp( Column 'VALUE' ) in each group

 

 

3.5.2. Without members : With this we will not have access to group elements
Here I have created work area as an object reference , now it is also possible ...isn't it awesome

WRITE : /'Using Object reference as work area : '.
LOOP AT lt_target REFERENCE INTO DATA(lo_target)
                          GROUP BY ( value = lo_target->value
                                     size = GROUP SIZE        " Optional : Defines the number of rows in a group
                                     index = GROUP INDEX  " Optional : Index of group
                                    )
                            ASCENDING             "Optional - by default its ascending , groups can be sorted
            WITHOUT MEMBERS "Optional - If this is given then we can't access the group members
             REFERENCE INTO DATA(lo_grp).


* Here we will not have access to the group members ,
* So lets display the groups only


WRITE : / lo_grp->value , lo_grp->size, lo_grp->index .
ENDLOOP.

NOTE : Group are created based on value column , size in number of elements in a group and sequence is ascending as per size. Index is as per sequence of the group creation . 

As ‘SAP’  in VALUE column is found first hence index = 1 and hence for ‘ABAP2’ its 2 and for ‘ABAP3’ its 3.

Output after above loop :


Next - Loop with FOR


No comments:

Post a Comment