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

Quick 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,

  • for which the internal table itab is specified directly as a data object and not specified as the result of a call or expression,
  • in which the addition WITHOUT MEMBERS is not used.

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.

A member loop always sets the system field sy-subrc to 0.


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 angle 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. This exploits the internal variant of the assignment of the rows to their group.
  • A loop LOOP AT GROUP does not permit group level processing with the statement AT.
  • A group loop does not create any empty groups, which means that a member loop is always passed as a rule. Only WHERE conditions can be used to dictate that the members of a group are not processed. Internally, however, this is not detected until the start of the first loop pass, which means that sy-subrc is set to 0 in this case too.
  • The statement LOOP AT GROUP corresponds to the expression FOR ... IN GROUP. This means its functions can often be expressed more elegantly using table comprehensions or table reductions.

Example

Member loop in a group loop, where the flight numbers belonging to an airline are grouped together in a string.

SELECT * 
       FROM spfli 
       INTO TABLE @DATA(spfli_tab). 

LOOP AT spfli_tab INTO DATA(wa) 
                  GROUP BY wa-carrid 
                  INTO DATA(key). 
  cl_demo_output=>next_section( |{ key }| ). 
  DATA(str) = ``. 
  LOOP AT GROUP key ASSIGNING FIELD-SYMBOL(<members>). 
    str = str && ` ` && <members>-connid. 
  ENDLOOP. 
  cl_demo_output=>write( str ). 
ENDLOOP. 
cl_demo_output=>display( ). 

Example

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

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. 

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

Like the example above, but with a restricted WHERE condition for the member loop.

SELECT * 
       FROM spfli 
       INTO TABLE @DATA(spfli_tab). 

LOOP AT spfli_tab INTO DATA(wa) 
                  GROUP BY wa-carrid 
                  INTO DATA(key). 
  cl_demo_output=>next_section( |{ key }| ). 
  DATA(str) = ``. 
  LOOP AT GROUP key ASSIGNING FIELD-SYMBOL(<members>) 
                    WHERE cityfrom = 'NEW YORK'. 
    str = str && ` ` && <members>-connid. 
  ENDLOOP. 
  cl_demo_output=>write( str ). 
ENDLOOP. 
cl_demo_output=>display( ).