ABAP Keyword Documentation → ABAP - Reference → Processing Internal Data → Internal Tables → Processing Statements for Internal Tables → LOOP AT itab
LOOP AT itab - GROUP BY
Other versions:
7.31 | 7.40 | 7.54
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:
-
Grouping
In the first phase, all rows specified by the conditionscond
are read in the processing order specified in a loop across rows. The statements in the statement block betweenLOOP
andENDLOOP
are not executed during this operation. For each row read, a group key is constructed in the group key expressiongroup_key
instead. Each group key represents a group. Each row is assigned to precisely one group (as a member). This is the group of all rows with the same group key. If the additionWITHOUT MEMBERS
is not used, this assignment is internal and can be used for access to the members of a group in the second phase. -
Group Loop
In the second phase, a loop is executed across all groups. The statements in the statement block betweenLOOP
andENDLOOP
are executed for each loop pass. The output behavior for the group loop is defined ingroup_result
and the corresponding values can be accessed in the loop. If the assignment of the table rows to their groups is defined, a member loopLOOP AT GROUP
can be nested in theLOOP
to read the rows in each group.
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 default order of the groups is based on the time their group key is first created, which itself can be overridden using the additions
ASCENDING
orDESCENDING
. -
If the assignment of the rows to their group is defined, the order of the rows of a group is based on the time they are assigned to the group. This defines, in particular, the first row of each group, used as a representative in the
representative binding.
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:
-
If a representative binding is defined in the output
behavior,
sy-tabix
is set to the value that would be set for the row representing the group in theLOOP
without grouping. -
If a group key binding is defined in the output
behavior, the groups are counted in
sy-tabix
. The first loop pass setssy-tabix
to 1 and each subsequent loop pass raises it by 1.
The same applies to sy-subrc
as in a loop across rows.
Notes
-
A grouping (the assignment of rows to a group) exists only within the group loop and a group can only
be addressed here after LOOP
AT GROUP or
FOR ... IN GROUP
. -
A
LOOP
with the additionGROUP BY
is not possible for mesh paths. -
No control level
processing with the statement
AT
is possible in aLOOP
with the additionGROUP BY
. -
Unlike in control
level processing, a grouping with
GROUP BY
is not defined by the structure of the rows and the processing order of the loop. A grouping withGROUP BY
can usually replace control level processing in cases where the internal table is sorted before the loop by the group key (see the example). -
Internal tables can also be group using the expression
FOR GROUPS ... OF
.
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.
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,
- the output behavior cannot be defined for a row of the group as a representative, a group key binding must be defined,
-
the group loop cannot contain a nested member loop
LOOP AT GROUP
, -
the internal table
itab
cannot be modified in the group loop.
Note
The addition WITHOUT MEMBERS
is used to improve performance in all cases where the content of the groups is not required.
Example
See the examples for grouping internal tables.