What does it do? This variant of the statement LOOP AT itab>> groups the lines 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 lines> 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 lines determined by the conditions cond>> are read in the processing order described in a loop across lines>. The statements in the statement block between LOOP> and ENDLOOP> are not executed during this process. For each line read, a group key is constructed in the group key expression group_key>> instead. Each group key represents a group. Each line is assigned to exactly one group as a member, namely the group of all lines with the same group key. If the addition WITHOUT 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 between LOOP> and ENDLOOP> are executed for each loop pass. The output behavior for the group loop is defined in group_result> > and the corresponding values can be accessed in the loop. If the assignment of the table lines to their groups is defined, a member loop LOOP AT GROUP>> can be nested in the LOOP> to read the lines in each group. The default order of the groups in the group loop as well as 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 depends on the time their group key is first created, which itself can be overridden by an explicit sorting using the additions ASCENDING> or DESCENDING>.
If the assignment of the lines to their group is defined, the order of the lines of a group is based on the time they are assigned to the group. This defines, in particular, the first line of each group that is 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 line representing the group in the LOOP> without grouping.
If a group key binding is defined in the output behavior>, the groups are counted in sy-tabix>. The first loop pass sets sy-tabix> to 1 and each subsequent loop pass raises it by 1. 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 lines>.
Latest notes:
A grouping, that is, the assignment of lines to a group, only exists within the group loop and a group can only be addressed after LOOP AT GROUP>> or FOR ... IN GROUP>>.
A member loop LOOP AT GROUP>> is only possible in LOOP> statements for which the internal table itab > is specified directly as a data object and not as the result of a call or expression. If the table is specified as the result of a call or expression, only the group keys exist in the group loop and no longer the groups themselves.
A LOOP> with the addition GROUP BY> is not possible for mesh paths>.
No group level processing> with the statement AT>> is possible in a LOOP> with the addition GROUP BY>.
Internal tables can also be grouped using the expression FOR GROUPS ... OF>>.
Unlike in group level processing >, a grouping with GROUP BY> does not depend on the structure of the lines and the processing order of the loop. A grouping with GROUP BY> can usually replace group level processing in cases where the internal table is sorted before the loop by the group key.
Groupings using GROUP BY> replace self-programmed group loops. NON_V5_HINTS
In this example>, GROUP BY> is used to replace group level processing.
For groupings using GROUP BY> to replace self-programmed group loops see the example>). ABAP_HINT_END
ABAP_EXAMPLE_VX5 The example shows the simplest form of grouping by a 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 line of each group and represents the group in the loop. This is called a representative binding. ABEXA 00394 ABAP_EXAMPLE_END
ABAP_EXAMPLE_ABEXA Grouping Internal Tables> ABAP_EXAMPLE_END • ASCENDING LOOP AT GROUP BY • DESCENDING LOOP AT GROUP BY • AS TEXT LOOP AT GROUP BY
ABAP Addition
What does it do? 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.
Latest notes: Groups can be sorted as an addition to the statement SORT>> if the criteria here are not sufficient. NON_V5_HINTS ABAP_HINT_END
ABAP_EXAMPLE_VX5 Like the example above, but with sorting by group key in descending order. ABEXA 00395 ABAP_EXAMPLE_END
ABAP_EXAMPLE_ABEXA Grouping with LOOP> and Sort>. ABAP_EXAMPLE_END • WITHOUT MEMBERS LOOP AT GROUP BY
ABAP Addition
What does it do? The addition WITHOUT MEMBERS> deactivates the default internal variant of the assignment of each table line to its group. This addition constructs groups, but there is no access to the lines of the groups in the group loop. If the addition WITHOUT MEMBERS> is specified, the following applies:
The output behavior> cannot be defined for a line of the group as a representative and a group key binding must be defined.
The group loop cannot contain a nested member loop LOOP AT GROUP>>.
The internal table itab> can be modified in the group loop.
Latest notes: The addition WITHOUT MEMBERS> is used to improve performance in all cases where the content of the groups is not required. NON_V5_HINTS ABAP_HINT_END
ABAP_EXAMPLE_VX5 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 lines of the groups in the loop. ABEXA 00396 ABAP_EXAMPLE_END