SAP LOOP AT ITAB GROUP BY KEY ABAP Statements



Get Example source ABAP code based on a different SAP table
  


• GROUP SIZE LOOP AT GROUP BY
• GROUP • LOOP AT GROUP BY

LOOP AT itab, group_key

ABAP_SYNTAX
... key $| ( key1 = dobj1 key2 = dobj2 ...
$[gs = GROUP SIZE$] $[gi = GROUP •$] ) ...

ABAP Addition
1 ... gs = GROUP SIZE
2 ... gi = GROUP •

What does it do?
Group key expression for constructing the group key in the grouping of internal tables. The group key is the result of the group key expression, whose data type can be defined as follows:
If key is specified, the group key is a single data object with any data type.
If ( key1 = dobj1 key2 = dobj2 ... ) is specified, the group key is a structure with the components key1, key2, ... The components can have any name and any data type. The structure type is defined here as a bound data type of the structure.
key or key1, key2, ... are general expression positions in which the current line can be used in accordance with the output behavior defined in result. The data types of key or key1, key2, ... must be known completely and statically.
For each line of the internal table itab read in the grouping phase, the value of the group key is calculated from the data objects, expressions, or calls specified here using the group key expression.
If a group key binding is defined in the output behavior group_result of the group loop, the data object or field symbol specified here can be used to access the value of the group key of every group in the group loop.
BEGIN_SECTION SAP_INTERNAL_HINT
For each LOOP block with GROUP BY internally an anonymous hashed table is created, known as the group key table. The group key table is available during the LOOP processing only. Its table key is the group key. Each group key adds a line to this table and the visible loop is processed for the table.
END_SECTION SAP_INTERNAL_HINT



Latest notes:

The calculated value of the group key for each read line of itab must depend on the appropriate type of the read line to achieve a useful grouping. The following are some special cases:
If the group key is a value independent of the line, there is only one group to which all read lines belong.
If the group key is different for each line, there are as many groups as lines and each group contains only one line. The simplest solution is to directly assign the values of the lines to the group key or its components. However, since they are general expression positions on the right side, any type of evaluation is possible.
The ability to directly construct any type of structure makes it easy to create multicomponent group keys without having to declare a matching structure first or to concatenate the key components into, for example, a string.
The syntax shown here applies both to groupings with LOOP AT itab ... GROUP BY and to groupings with FOR GROUPS ... OF.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
A structured group key is used to group by more than just one criterion. The group criteria here are simply columns of the internal table. This is a representative binding in which the work area wa is reused in the group loop to access the group key.
ABEXA 00399
ABAP_EXAMPLE_END

ABAP Addition

ABAP Addition

What does it do?
Declares additional components for a structured group key. These components are not part of the group key and are used to store group-specific information instead:
The component gs is filled with the number of group members for each group.
The component gi is filled with a group index for each group. The group index of the group key constructed first is 1 and is raised by 1 for each new group key.
Before the additional components can be declared, a group key binding must be defined in the output behavior group_result. The additional components can be accessed in the group loop using the data object or field symbol specified here.
The names of the components gs and gi can be freely defined. The language elements GROUP SIZE or GROUP • on the right side give the components their special semantics.



Latest notes:

If the groups are not sorted using ASCENDING or DESCENDING , the groups in the group loop are processed in the order of the group index. If the groups are sorted differently, the original position is noted in the group index.
The additional components can still be evaluated even if the addition WITHOUT MEMBERS is used and the group members cannot be accessed.
Implicitly, no further values are created that match the results of aggregate expressions when GROUP BY is used in the AB-SQL statement SELECT, except in the component gs with the right side GROUP SIZE. Aggregations like this are easy to program particularly when applying constructor operators such as REDUCE to the group members. The AB-SQL addition HAVING for restricting the loops of groups is a similar case. Here, for example, the filter operator FILTER can be used.
NON_V5_HINTS
For the latter see the example
ABAP_HINT_END

ABAP_EXAMPLE_VX5
Declaration of a structured group key with additional components for group index and group size. A group key binding must be defined with INTO data(key).
ABEXA 00400
ABAP_EXAMPLE_END

Return to menu