Ansicht
Dokumentation

ABAPLOOP_AT_ITAB_GROUP_BY_KEY - LOOP AT ITAB GROUP BY KEY

ABAPLOOP_AT_ITAB_GROUP_BY_KEY - LOOP AT ITAB GROUP BY KEY

BAL_S_LOG - Application Log: Log header data   ROGBILLS - Synchronize billing plans  
This documentation is copyright by SAP AG.
SAP E-Book

LOOP AT itab, group_key

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


Additions

1. ... gs = GROUP SIZE

2. ... gi = GROUP INDEX

Effect

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.

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.

Example

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.

Addition 1

... gs = GROUP SIZE

Addition 2

... gi = GROUP INDEX

Effect

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 INDEX on the right side give the components their special semantics.

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 statement SELECT, except in the component gs with the right side GROUP SIZE. Aggregations like this are easy to program (see the example), particularly when applying constructor operators such as REDUCE to the group members. The addition HAVING for restricting the loops of groups is a similar case. Here, for example, the filter operator FILTER can be used.

Example

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






PERFORM Short Reference   BAL Application Log Documentation  
This documentation is copyright by SAP AG.

Length: 7396 Date: 20240419 Time: 070737     sap01-206 ( 114 ms )