Skip to content

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

LOOP AT itab - group_key

Other versions: 7.31 | 7.40 | 7.54

Syntax


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

Extras

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 is 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 names and any data types. 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 row 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 row 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

  • If the grouping is to be useful, the value of the group key calculated for every read row of itab must have a suitable relationship to the row in question. The following are some special cases:

  • If the group key is a value that depends on the row, there is only one group to which all read rows belong.

  • If the group key is different for every read row, there are the same number of groups as rows and each group contains only one row.
The simplest cases are direct assignments of values of the row to the group key or its components. The right side comprises general expression positions, which means that any type of evaluation is possible.
  • The ability to directly construct any type of structure makes it easy to create multi-component group keys without needing to declare a matching structure first or to concatenate the key components in, 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.


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.

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

LOOP AT spfli_tab INTO DATA(wa) 
                  GROUP BY ( key1 = wa-carrid key2 = wa-airpfrom ). 
  cl_demo_output=>write( |{ wa-carrid } { wa-airpfrom }| ). 
ENDLOOP. 
cl_demo_output=>display( ). 

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 as a repository of 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 components gs and gi can be given any name. 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 in different ways, 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 ABAP SQL 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 ABAP SQL 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).

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

LOOP AT spfli_tab INTO DATA(wa) 
                  GROUP BY ( key1 = wa-carrid key2 = wa-airpfrom 
                             indx = GROUP INDEX 
                             size = GROUP SIZE ) 
                  INTO data(key). 
  cl_demo_output=>write( |{ key-indx } { key-key1 
                       } { key-key2 } { key-size }| ). 
ENDLOOP. 
cl_demo_output=>display( ).