Get Example source ABAP code based on a different SAP table
• GROUPS OF FOR • GROUP BY FOR GROUPS • WITHOUT MEMBERS FOR GROUPS • ASCENDING FOR GROUPS • DESCENDING FOR GROUPS • AS TEXT FOR GROUPS • • INTO FOR GROUPS
FOR GROUPS ... OF>
ABAP_SYNTAX ... FOR GROUPS $[group$| < group>$] OF wa$|<(><)> IN itab $[ • INTO idx$] $[cond>$] GROUP BY group_key> $[ASCENDING$|DESCENDING $[AS TEXT$]$] $[WITHOUT MEMBERS$] $[let_exp>$] ...>
ABAP Addition ... • INTO idx>
What does it do? 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:
Grouping of all lines 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 line read is either written to the local work area wa> or assigned to the local field symbol <(><)>> and can then be evaluated when the group key is constructed.
Evaluation of the groups. Specifying group> or < group>> (optional) results in the group key binding> in which the current group key is written to the local data object group> or assigned to the local field symbol < group>>. If group> or < group>> is not specified, the representative binding> applies, in which the first line of a group is written to the local work area wa> or assigned to the local field symbol <(><)>>. In the group key binding, wa> or <(><)>> 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 type of binding, the work area wa> or group > or the field symbol <(><)>> 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.
Latest notes: If a constructor expression contains a FOR> expression with group key binding and the group key is to be used as the result of the constructor expression, a suitable data type or a data object with this type must exist for this expression explicitly. It is not possible to derive this type here using an inline declaration as in LOOP AT>>. NON_V5_HINTS ABAP_HINT_END
ABAP_EXAMPLE_VX5 Display of all values of the column carrid> that occur 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>. ABEXA 01034 ABAP_EXAMPLE_END
ABAP Addition
What does it do? For each evaluated group, this addition sets the helper variable idx > as follows:
In the case of representative bindings>, to the value that would be set by the addition • INTO> in evaluations using FOR ... IN itab> > for the representative line.
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>>.
ABAP_EXAMPLE_VX5 Grouping and sorting of an internal table with random numbers by the numbers and sorts it. The differences between the group number in i > and the random number demonstrate the deviation from the uniform distribution. ABEXA 01035 ABAP_EXAMPLE_END