Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Internal Tables →  Expressions and Functions for Internal Tables →  FOR - Table Iterations 

FOR GROUPS ... OF

Other versions: 7.31 | 7.40 | 7.54

Syntax


... FOR GROUPS [group|<group>] OF wa|<fs> IN itab 
        [INDEX INTO idx] [cond]
        GROUP BY group_key
        [ASCENDING|DESCENDING [AS TEXT]]
        [WITHOUT MEMBERS]
        [let_exp] ...

Addition

... INDEX INTO idx

Effect

This variant of an iteration expression for table iterations with FOR evaluates an internal table itab in the same way as a LOOP with the addition GROUP BY in two phases:

  • Groups all rows that meet the condition in cond by the group key specified in group_key. The same applies to group_key as in LOOP AT ...GROUP BY. In this phase, each row read is either written to the local work area wa or assigned to the local field symbol <fs>. This can then be evaluated when the group key is constructed.
  • Evaluates the groups. Specifying group or <group> (optional) applies the group key binding used to write the current group key to the local data object group or assign it to the local field symbol <group>. If group or <group> is not specified, the representative binding applies, in which the first row of a group is written to the local work area wa or assigned to the local field symbol <fs>. In the group key binding, wa or <fs> are initial after the FOR expression.

itab is a functional operand position. The same applies to the additions ASCENDING, DESCENDING, and WITHOUT MEMBERS as in LOOP AT ... GROUP BY. In particular, WITHOUT MEMBERS can be specified only if a group key binding is defined after GROUPS by specifying group or <group>.

Depending on the binding method, the work area wa or group or the field symbol <fs> or <group> after the FOR expression can either be used in further subexpressions or to construct the result of a table comprehension or table reduction. In particular, it can be specified in the expression FOR ... IN GROUP to evaluate the members of the current group.


Note

If a constructor expression contains a FOR expression with group key binding and the group key is used as the result of the constructor expression, a suitable data type or a data object with this type must be specified for this expression explicitly. It is not possible to derive this type here using an inline declaration as in LOOP AT.


Example

Produces all values of the column carrid in spfli. The result of the table comprehension is a single-column internal table that contains all group key values. The FOR loop works with a group key binding. Here, each group key is assigned to the local data object carrier.

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

TYPES group_keys TYPE STANDARD TABLE OF spfli-carrid WITH EMPTY KEY. 

cl_demo_output=>display( 
  VALUE group_keys( 
    FOR GROUPS carrier OF wa IN spfli 
    GROUP BY wa-carrid 
    ASCENDING 
    WITHOUT MEMBERS 
    ( carrier ) ) ). 

Addition

... INDEX INTO idx

Effect

For each evaluated group, this addition sets the auxiliary variable idx as follows:

  • In the case of representative bindings, to the value that would be set by the addition INDEX INTO in evaluations using FOR ... IN itab for the representative row.
  • In the case of group key bindings, the groups are counted from the value 1.

The same applies to the local data object idx as in the variant FOR ... IN itab.

idx cannot be addressed in group_key.