SAP LOOP GROUP BY ABEXA
Get Example source ABAP code based on a different SAP table
ABAP_ITAB - Grouping Internal Tables, Step by Step
The example is a step-by-step introduction to grouping with
ABAP_SOURCE_CODE
DEMO CL_DEMO_LOOP_AT_ITAB_GROUP_BY
ABAP_DESCRIPTION
Similar to SQL's
While there are more examples for all the powerful features of
the above example shows the grouping of internal tables step by step using a very simple case of an internal table
Grouping by One Column
The simplest form of grouping is by one column without explicitly specifying the output behavior of the group loop:
GROUP BY wa-carrid.
... wa-carrid ...
ENDLOOP.>
Within the loop, there is access to the work area
To access the members of a group, a member loop can be inserted into the group loop:
GROUP BY wa-carrid.
...
LOOP AT GROUP wa INTO DATA(member).
... member-... ...
ENDLOOP.
...
ENDLOOP.>
The member loop is executed using the group represented by
Grouping by More than One Column
To group by more than just one criterion, a structured group key is defined as follows. In the simplest case, the grouping criteria are columns of the internal table:
GROUP BY ( key1 = wa-carrid key2 = wa-airpfrom ).
... wa-carrid ... wa-airpfrom ...
ENDLOOP.>
This is also a representative binding in which the work area
To access the members of the groups, the exact same member loop can be inserted as when grouping by one column.
Group Key Binding when Grouping by One Column
By explicitly specifying an
GROUP BY wa-carrid
INTO DATA(key).
... key ...
ENDLOOP.>
The difference to the example with representative binding is the
Inserting a member loop works in the same way as in the representative binding, with the difference that a group is now addressed by
GROUP BY wa-carrid
INTO key.
...
LOOP AT GROUP key INTO member.
... members ...
ENDLOOP.
...
ENDLOOP.>
Group Key Binding when Grouping by More than One Column
Finally, the group key binding for structured group keys:
GROUP BY ( key1 = wa-carrid key2 = wa-airpfrom )
INTO DATA(key).
... key-key1 ... key-key2 ...
ENDLOOP.>
Here,
If the group members are not relevant, the addition
GROUP BY ( key1 = wa-carrid key2 = wa-airpfrom
index = GROUP • size = GROUP SIZE )
WITHOUT MEMBERS
INTO DATA(key).
... key-key1 ... key-key2 ... key-index ... key-size ...
ENDLOOP.>
It is no longer possible to use a member loop here. Instead, the group key was enriched with optional components for further information using