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

LOOP AT itab - GROUP BY

Quick Reference

Syntax

LOOP AT itab result [ cond] GROUP BY group_key
                           [ASCENDING|DESCENDING [AS TEXT]]
                           [WITHOUT MEMBERS]
                           [group_result].
  ...
  [LOOP AT GROUP ...
    ...
  ENDLOOP.]
  ...
ENDLOOP.


Extras:

1. ... ASCENDING|DESCENDING [AS TEXT]

2. ... WITHOUT MEMBERS

Effect

This variant of the statement LOOP AT itab groups the rows of the internal table and executes a loop across the groups. The same applies to the syntax of the additions result and cond as to a loop across rows with the exception that the addition TRANSPORTING NO FIELDS cannot be specified.

If the addition GROUP BY is specified, the LOOP is processed in two phases:

The default order of the groups in the group loop plus the order of the members within a group is defined by the processing order of the LOOP in the first phase:

The internal table itab cannot be modified in the group loop unless the addition WITHOUT MEMBERS is specified.

System Fields

This variant of the statement LOOP AT with the addition GROUP BY sets the values of the system field sy-tabix in the group loop as follows:

After leaving the loop using ENDLOOP, sy-tabix is set to the value that it had before entering the loop. The same applies to sy-subrc as in a loop across rows.

Notes

Example

The example shows the simplest form of grouping: by one column, without explicitly specifying the output behavior of the group loop: Within the loop, there is access to the work area wa, in particular to the component wa-carrid that is used for grouping. The work area wa contains the first row of each group and represents the group in the loop. This is called a representative binding.

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

LOOP AT spfli_tab INTO DATA(wa)
                  GROUP BY wa-carrid.
  cl_demo_output=>write( wa-carrid ).
ENDLOOP.
cl_demo_output=>display( ).

Executable Examples

Grouping Internal Tables

Addition 1

... ASCENDING|DESCENDING [AS TEXT]

Effect

These additions sort the groups by the group key in ascending or descending order before the group loop is executed. The groups are sorted in exactly the same way as when the statement SORT is used on an internal table whose primary table key is the group key and the addition AS TEXT is applied accordingly.

The group loop is executed in the sort order. If the additions ASCENDING and DESCENDING are not specified, the groups are in the order in which the value of a group key was constructed for the first time.

Note

Groups can be sorted as an addition to the statement SORT if the criteria here are not enough.

Example

Like the example above, but with sorting by group key in descending order.

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

LOOP AT spfli_tab INTO DATA(wa)
                  GROUP BY wa-carrid DESCENDING.
  cl_demo_output=>write( wa-carrid ).
ENDLOOP.
cl_demo_output=>display( ).

Executable Example

^Grouping with LOOP and Sort.

Addition 2

... WITHOUT MEMBERS

Effect

The addition WITHOUT MEMBERS deactivates the default internal variant of the assignment of each table row to its group. This addition constructs groups but there is not access to the rows of the groups in the group loop. If the addition WITHOUT MEMBERS is specified,

Note

The addition WITHOUT MEMBERS is used to improve performance in all cases where the content of the groups is not required.

Example

Like the example above, but with the addition WITHOUT MEMBERS, for which a group key binding is defined with INTO DATA(key). There is no access to the rows of the groups in the loop.

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

LOOP AT spfli_tab INTO DATA(wa)
                  GROUP BY wa-carrid WITHOUT MEMBERS
                  INTO DATA(key).
  cl_demo_output=>write( key ).
ENDLOOP.
cl_demo_output=>display( ).



Continue
LOOP AT itab - group_key
LOOP AT itab - group_result
LOOP AT GROUP
Examples of Grouping with LOOP