ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Internal Tables → Expressions and Functions for Internal Tables → FOR - Table Iterations
FOR ... IN GROUP
Other versions:
7.31 | 7.40 | 7.54
Syntax
... FOR { wa|<fs> IN GROUP group [INDEX INTO idx] [
WHERE ( log_exp )] }
| { GROUPS OF
wa|<fs> IN GROUP group [INDEX INTO idx] [
WHERE ( log_exp )]
GROUP BY group_key
[ASCENDING|DESCENDING [AS TEXT]]
[WITHOUT MEMBERS] } [
let_exp] ...
Addition
Effect
These variants of an iteration expression for
table iterations using
FOR
evaluate a row group group
sequentially, like a
member loop
LOOP AT GROUP
. These FOR
expressions can only be specified in those places in which a group group
can be accessed, namely:
- in a constructor expression after an expression
FOR GROUPS ... OF
,
- in a
LOOP
with the additionGROUP BY
.
where in both cases
- the internal table must be specified directly and not specified as the result of an expression or a function,
- the addition
WITHOUT MEMBERS
cannot be specified.
group
is used to specify the group in question using the data object or field symbol to which the group is bound.
- In the variant
FOR ... IN GROUP
withoutGROUPS OF
, the expression evaluates the members of the current group in the same way as the variantFOR ... IN itab
evaluates the rows of the internal tableitab
. The result is the rows of the group.
- In the variant
FOR GROUPS OF ... IN GROUP
, the expression evaluates the members of the current group in the same way as the variant FOR GROUPS ... OF. The current group is grouped again and the result is either representatives or group keys of the subgroups.
The condition WHERE ( log_exp
) can be used to restrict the evaluated rows of the group. Conditions other than this static WHERE
condition cannot be specified.
Note
As in LOOP AT GROUP
, access to the groups is optimized internally.
Example
Displays a list of the total occupied seats for each airline. A LOOP
with
the addition GROUP BY
evaluates its groups in a table reduction using FOR flight IN GROUP <carrier>
. Here, the total for each group is calculated for the column seatsocc
.
SELECT * FROM sflight INTO TABLE @DATA(sflight).
TYPES group_keys TYPE STANDARD TABLE OF spfli-carrid WITH EMPTY KEY.
TYPES:
BEGIN OF list_line,
carrier TYPE sflight-carrid,
bookings TYPE i,
END OF list_line,
list TYPE STANDARD TABLE OF list_line WITH EMPTY KEY.
DATA(list) = VALUE list( ).
LOOP AT sflight INTO DATA(wa)
GROUP BY wa-carrid
ASCENDING
ASSIGNING FIELD-SYMBOL(<carrier>).
list = VALUE list(
BASE list
( carrier = <carrier>
bookings = REDUCE #(
INIT s = 0
FOR flight IN GROUP <carrier>
NEXT s = s + flight-seatsocc ) ) ).
ENDLOOP.
cl_demo_output=>display( list ).
Addition
... INDEX INTO idx
Effect
For each row of the associated FOR
expression, this addition sets the helper
variable idx
to the value to which the system field sy-tabix
would be set in a corresponding LOOP
AT GROUP loop. The same applies to the local data object idx
as in the variant FOR ... IN itab
.