Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Internal Tables →  Processing Statements for Internal Tables →  LOOP AT itab →  LOOP AT itab - GROUP BY 

LOOP AT itab - group_result

Other versions: 7.31 | 7.40 | 7.54

Syntax


 ... { } 
  | {INTO group}|{ASSIGNING <group>}|{REFERENCE INTO group_ref} ...

Alternatives

1. ... { }

2. ... {INTO group}|{ASSIGNING <group>}|{REFERENCE INTO group_ref}

Effect

Defines the output behavior of the group loop in a grouping of an internal table in a LOOP. The output behavior defines the following:

  • A data object or field symbol whose content is accessed in the group loop and which can also be passed to called procedures.
  • A name for the group which can only be used within the current group loop (and loops nested here) in the statement LOOP AT GROUP or in an expression FOR ... IN GROUP for specifying a group.

Alternative 1

... { }

Effect

Representative binding. If nothing is specified for the output behavior group_result, the output behavior is determined for the table rows using the addition result. In each loop pass, the first row of the current group is assigned as a representative of its group to the data object or field symbol defined there. The representative is bound to the current group and the group can be addressed using the name of the representative in the statement LOOP AT GROUP or the expression FOR ... IN GROUP.

If the representative binding is used,

  • the addition WITHOUT MEMBERS is not possible, since there would be no assignment of the representative to its group in this case,
  • no additional components for GROUP SIZE and GROUP INDEX can be created for a structured group key, since it is impossible to access these components in this case.

Outside of the group loop, the representative is not bound to the group and cannot be used to specify it in member loops. This applies in particular to procedures called from the group loop.


Note

If a representative is bound to the current group, the associated group key in cannot be addressed in the group loop.


Example

The group key in the following example is a structure with two components key1 and key2 to which the columns key1 and key2 respectively are assigned to each row of the internal table itab in the grouping. This produces three groups with the group key values (a, a), (a, b), and (b, a). Accordingly, the group loop is passed three times and the first, second, and sixth row are assigned to the field symbol <wa> as representatives. See also the executable example of output behavior.

TYPES: 
  BEGIN OF struct, 
    key1 TYPE string, 
    key2 TYPE string, 
    col      TYPE i, 
  END OF struct, 
  itab TYPE STANDARD TABLE OF struct WITH EMPTY KEY. 

DATA(itab) = VALUE itab( 
  ( key1 = `a` key2 = `a` col = 1 ) 
  ( key1 = `a` key2 = `b` col = 2 ) 
  ( key1 = `a` key2 = `a` col = 3 ) 
  ( key1 = `a` key2 = `a` col = 4 ) 
  ( key1 = `a` key2 = `b` col = 5 ) 
  ( key1 = `b` key2 = `a` col = 6 ) 
  ( key1 = `b` key2 = `a` col = 7 )  ). 

LOOP AT itab ASSIGNING FIELD-SYMBOL(<wa>) 
             GROUP BY ( key1 = <wa>-key1 key2 = <wa>-key2 ). 
  cl_demo_output=>write( <wa> ). 
ENDLOOP. 

cl_demo_output=>display( ). 

Alternative 2

... {INTO group}|{ASSIGNING <group>}|{REFERENCE INTO group_ref}

Effect

Group key binding. If an explicit output behavior group_result is specified, the group key of the current key is assigned to the data object or field symbol specified after INTO, ASSIGNING, or REFERENCE INTO in every loop pass. Here, the same syntax and semantics apply as in the output behavior result. The data type of the specified data object or field symbol must match the data type of the group key. If the group key is constructed as a new structure in the group key expression, the row type must be a structure whose components exist with the same name and in the same order as in the group key.

The specified data object or field symbol is bound to the current group and the group can be addressed using its name in the statement LOOP AT GROUP or the expression FOR ... IN GROUP. Outside of the group loop, the data object or field symbol is not bound to the group and cannot be used to specify it in member loops. This applies in particular to procedures called from the group loop. When the group loop is closed, the grouping is canceled and a specified data object is initialized or a field symbol no longer has a group key assigned to it.

The data object or field symbol of the output behavior defined in result for the table rows can still be addressed in the group loop but it is either initial after the LOOP statement or does not point to a row.


Notes

  • To specify a type-friendly data object or field symbol as a target for the group key, it is best to use an inline declaration. This can always be done here.
  • The output behavior result for the table rows is still needed for the construction of the group key group_key if group_result is specified explicitly.
  • Unlike in result for table rows, no addition CASTING can be specified after ASSIGNING.

Example

Like in the previous example, however the groups are not assigned to an implicit representative here and are assigned to the explicitly defined target area for the group key instead. In each of the three loop passes, the reference variable group_key declared inline points to the structured group key (a, a), (a, b), and (b, a) in question. See also the executable example of output behavior.

TYPES: 
  BEGIN OF struct, 
    key1 TYPE string, 
    key2 TYPE string, 
    col      TYPE i, 
  END OF struct, 
  itab TYPE STANDARD TABLE OF struct WITH EMPTY KEY. 

DATA(itab) = VALUE itab( 
  ( key1 = `a` key2 = `a` col = 1 ) 
  ( key1 = `a` key2 = `b` col = 2 ) 
  ( key1 = `a` key2 = `a` col = 3 ) 
  ( key1 = `a` key2 = `a` col = 4 ) 
  ( key1 = `a` key2 = `b` col = 5 ) 
  ( key1 = `b` key2 = `a` col = 6 ) 
  ( key1 = `b` key2 = `a` col = 7 )  ). 

LOOP AT itab ASSIGNING FIELD-SYMBOL(<wa>) 
             GROUP BY ( key1 = <wa>-key1 key2 = <wa>-key2 ) 
             WITHOUT MEMBERS 
             REFERENCE INTO DATA(group_key). 
  cl_demo_output=>write( group_key->* ). 
ENDLOOP. 

cl_demo_output=>display( ).