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 GROUP

Short Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


LOOP AT GROUP group result
[WHERE log_exp] [GROUP BY ...]. 
  ...
ENDLOOP.

Addition

... WHERE log_exp

Effect

Member loop across the rows of a group within the group loop in the grouping of internal tables. This loop is only possible within a LOOP across an internal table with the addition GROUP BY in which the addition WITHOUT MEMBERS is not specified.

group is used to specify the group across which the member loop passes. The target object must be specified that is defined in the output behavior group_result of the group loop and bound to the group:

  • In the representative binding, this is a representative specified in the output behavior result.
  • In the group key binding, this is a data object or field symbol specified in the output behavior group_result.

The member loop across the current group is executed just like a regular LOOP across a standard table with the row type of itab (which contains the rows of the group). Both variants are possible:

The second option enables further groupings of existing groups to be made.

System Fields

The statement LOOP AT GROUP sets the value of the system field sy-tabix in the member loop to the value that would be set for the current row in the LOOP without grouping.

The same applies to sy-subrc as in LOOP AT itab.


Notes

  • Despite the fact that there is always only one grouping for a single LOOP, it is still necessary to specify the group group explicitly, since multiple groups can be accessed in nested LOOPs.
  • group expects the precise name specified in the output behavior, namely a field symbol with pointed brackets specified after ASSIGNING and a data reference variable without dereferencing operator -> specified after REFERENCE INTO.
  • The statement LOOP AT GROUP can only be specified within a loop defined using LOOP AT ... GROUP BY, since the name for the group defined using its output behavior is only valid here. LOOP AT GROUP is, moreover, not possible in procedures called from a group loop, even if the data object or field symbol defined by the output behavior can be accessed here.
  • Despite the syntax and semantics of LOOP AT GROUP being the same as a regular nested loop, the way the rows of the group are actually accessed is optimized, making use of the internal variant of the assignment of the rows to their group.
  • A loop LOOP AT GROUP does not permit control level processing with the statement AT.
  • The statement LOOP AT GROUP is matched by the expression FOR ... IN GROUP, which means that its function can often be expressed more elegantly by table comprehensions or table reductions

Addition

... WHERE log_exp

Effect

The rows read from the group can be restricted using a static WHERE condition. The syntax and semantics are the same as in a LOOP across an internal table with the row type of itab.


Note

The additions USING KEY, FROM, TO, and a dynamic WHERE condition cannot be specified after LOOP AT GROUP.


Example

See the examples for grouping internal tables.


Example

The following example shows the six different methods of specifying a group in a member loop:

See also the executable example for output behavior.

TYPES itab TYPE STANDARD TABLE OF i WITH EMPTY KEY. 

DATA(itab) = VALUE itab( ( 1 ) ( 1 ) ( 2 ) ( 2 ) ). 

LOOP AT itab INTO DATA(wa) GROUP BY wa. 
  LOOP AT GROUP wa INTO DATA(member). 
    ... 
  ENDLOOP. 
ENDLOOP. 

LOOP AT itab ASSIGNING FIELD-SYMBOL(<fs>) GROUP BY <fs>. 
  LOOP AT GROUP <fs> INTO member. 
    ... 
  ENDLOOP. 
ENDLOOP. 

LOOP AT itab REFERENCE INTO DATA(dref) GROUP BY dref->*. 
  LOOP AT GROUP dref INTO member. 
    ... 
  ENDLOOP. 
ENDLOOP. 

LOOP AT itab INTO wa GROUP BY wa 
             INTO DATA(group). 
  LOOP AT GROUP group INTO member. 
    ... 
  ENDLOOP. 
ENDLOOP. 

LOOP AT itab INTO wa GROUP BY wa 
             ASSIGNING FIELD-SYMBOL(<group>). 
  LOOP AT GROUP <group> INTO member. 
    ... 
  ENDLOOP. 
ENDLOOP. 

LOOP AT itab INTO wa GROUP BY wa 
             REFERENCE INTO DATA(group_ref). 
  LOOP AT GROUP group_ref INTO member. 
    ... 
  ENDLOOP. 
ENDLOOP.