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

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,

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:

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

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( ).