LOOP AT – GROUP

Index

 

ABAP HANA 7.51: LOOP AT – GROUP

LOOP AT syntax is further extended to LOOP AT – GROUP statement. This empowers LOOP to aggregate Internal tables based on specified columns. This also gives freedom to loop only on the required group and provides runtime information about the Internal table grouping(Group size and members) and INDEXing.

Sample Source Code: Variant 1

*”Loop at GROUP BY provides better control than Classical ABAP LOOPs. These must be used in complex loop situations. E.g. Idoc scenarios

 

DATA : lt_idoc TYPE TABLE OF edidd,

       lt_seg  TYPE RANGE OF ediddsegnam.

 

lt_seg = VALUE #(
sign
=
‘I’ option = ‘EQ’ ( low = ‘E1EDK01’ )

                         ( low = ‘E1EDP01’ ) ).

 

lt_idoc = VALUE edidd_tt( docnum = 1 

                        ( segnam = ‘E1EDK01’ sdata = ‘1’ )” WHERE segnam IN lt_seg

                        ( segnam ‘E1EDK14’ sdata = ‘2’ )

                        ( segnam ‘E1EDK14’ sdata = ‘2’ )

                        ( segnam ‘E1EDP01’ sdata = ’02’ )“WHERE segnam IN lt_seg

                        ( segnam ‘E1EDP01’ sdata = ’01’ )“WHERE segnam IN lt_seg

                        ( segnam ‘E1EDP01’ sdata = ’03’ )“WHERE segnam IN lt_seg

                        ( segnam ‘E1EDP01’ sdata = ’04’ )“WHERE segnam IN lt_seg

                        ( segnam ‘E1EDS01’ sdata = ‘5’ ) ).

 

 

LOOP AT lt_idoc ASSIGNING
FIELD-SYMBOL(
<lwa_edidd>)                ” 1 Current loop work area assignment

    WHERE segnam IN lt_seg                   ” 2 ‘Where’ used for focused data

    GROUP BY ( segnam = <lwa_edidd>segnam   ” 3 Group by field

               size = GROUP SIZE             ” 4 This contains # of rows in <lfs_group>

               index = GROUP INDEX )         ” 5 This is index# of group iteration

    ASCENDING AS TEXT                        ” 6 Used to sorting Group data, here SEGNAM

” is used to improve performance but no access to the rows of the groups in the group loop

    “WITHOUT MEMBERS ” 7

    ASSIGNING FIELD-SYMBOL(<lfs_group>)  .   ” 8 Group assignment

 

  WRITE: / |Group Segment:{ <lfs_group>segnam  }|    &

           |      GROUP INDEX:{ <lfs_group>index }| &

           |      GROUP SIZE:{ <lfs_group>size }| &

           |      sy-tabix outside:{ sytabix }|.

 

  ” LOOP at Group – Special syntax

  LOOP AT GROUP <lfs_group> ASSIGNING
FIELD-SYMBOL(
<lfs_group_member>).

       WRITE : / |sy-tabix inside:{ sytabixsegnam:{ <lfs_group_member>segnam }| &

                 |sdata:<lfs_group_member>sdata }|.

  ENDLOOP.

  SKIP 2.

ENDLOOP.


Explanation Steps:

a. lt_seg has two segments(E1EDK01 AND E1EDP01) which will be used to filter LOOP data further

b. lt_idoc sample data is created with 8 segments.

c. A LOOP is initiated on lt_idoc

  1. Loop work area assignment is done as per old method
  2. Filter on Segment is used using WHERE condition with lt_seg table defined in step ‘a’
  3. Grouping is performed on lt_idoc using GROUP_BY keyword. Grouping is done on Segment name
  4. ‘size’ variable is defined which contains group size which is number of rows in the group
  5. ‘index’ variable is defined which contains iteration index for the outer loop
  6. ‘ASCENDING AS TEXT’ defined the group sorting. To understand this better, just change sample code to DESCENDING and execute again.
  7. ‘WITHOUT MEMBERS’ is an optional addition which is used when group data is not to be used inside the outer loop. e.g. in above code “LOOP AT GROUP <lfs_group>” will give a syntax error if ‘WITHOUT MEMBERS’ is uncommented
  8. <lfs_group> is defined here which contains group members. After this statement, <lfs_group> can be used to run inner loop for only group members.

TIP: It is suggested that you run and debug above code by yourself to understand this concept.

Output: Variant 1

out

Index

 

 

Leave a Reply