ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Internal Tables → Processing Statements for Internal Tables → LOOP AT itab → LOOP AT itab - GROUP BY → Examples of Grouping with LOOP
Internal tables, Introduction to LOOP AT GROUP BY
The example is a step-by-step introduction to grouping with LOOP AT GROUP BY
.
Other versions:
7.31 | 7.40 | 7.54
Source Code
DATA(out) = cl_demo_output=>new( ).
DATA:
wa TYPE spfli,
member TYPE spfli,
members TYPE STANDARD TABLE OF spfli WITH EMPTY KEY.
SELECT *
FROM spfli
INTO TABLE @DATA(spfli_tab).
out->begin_section( `Representative Binding` ).
out->begin_section(
`Grouping by one column` ).
LOOP AT spfli_tab INTO wa
GROUP BY wa-carrid.
out->write( wa-carrid ).
ENDLOOP.
out->next_section(
`Members of one column groups` ).
LOOP AT spfli_tab INTO wa
GROUP BY wa-carrid.
CLEAR members.
LOOP AT GROUP wa INTO member.
members = VALUE #( BASE members ( member ) ).
ENDLOOP.
out->write( members ).
ENDLOOP.
out->next_section(
`Grouping by two columns` ).
LOOP AT spfli_tab INTO wa
GROUP BY ( key1 = wa-carrid key2 = wa-airpfrom ).
out->write( |{ wa-carrid } { wa-airpfrom }| ).
ENDLOOP.
out->next_section(
`Members of two column groups` ).
LOOP AT spfli_tab INTO wa
GROUP BY ( key1 = wa-carrid key2 = wa-airpfrom ).
CLEAR members.
LOOP AT GROUP wa INTO member.
members = VALUE #( BASE members ( member ) ).
ENDLOOP.
out->write( members ).
ENDLOOP.
out->end_section( )->next_section( `Group Key Binding` ).
out->next_section(
`Grouping by one column` ).
LOOP AT spfli_tab INTO wa
GROUP BY wa-carrid
INTO DATA(key).
out->write( key ).
ENDLOOP.
out->next_section(
`Members of one column groups` ).
LOOP AT spfli_tab INTO wa
GROUP BY wa-carrid
INTO key.
CLEAR members.
LOOP AT GROUP key INTO member.
members = VALUE #( BASE members ( member ) ).
ENDLOOP.
out->write( members ).
ENDLOOP.
out->next_section(
`Grouping by two columns` ).
LOOP AT spfli_tab INTO wa
GROUP BY ( key1 = wa-carrid key2 = wa-airpfrom )
INTO DATA(keys).
out->write( keys ).
ENDLOOP.
out->next_section(
`Members of two column groups` ).
LOOP AT spfli_tab INTO wa
GROUP BY ( key1 = wa-carrid key2 = wa-airpfrom )
INTO keys.
CLEAR members.
LOOP AT GROUP keys INTO member.
members = VALUE #( BASE members ( member ) ).
ENDLOOP.
out->write( members ).
ENDLOOP.
out->next_section(
`Two column groups without members` ).
LOOP AT spfli_tab INTO wa
GROUP BY ( key1 = wa-carrid key2 = wa-airpfrom
index = GROUP INDEX size = GROUP SIZE )
WITHOUT MEMBERS
INTO DATA(keysplus).
out->write( keysplus ).
ENDLOOP.
out->display( ).
Description
An internal table spfli_tab
is filled with data from the database table SPFLI.
The program shows step by step how it can be grouped using LOOP AT GROUP BY
:
- Grouping by One Column
GROUP BY wa-carrid.
... wa-carrid ...
ENDLOOP.
wa
, in particular to the
component wa-carrid
that is used for grouping. The work area wa
contains the first row of each group and represents the group in the loop. This is called a representative binding.
GROUP BY wa-carrid.
...
LOOP AT GROUP wa INTO DATA(member).
... member-... ...
ENDLOOP.
...
ENDLOOP.
wa
and its members are assigned to member
, and are available in the member loop.
- Grouping by More Than One Column
GROUP BY ( key1 = wa-carrid key2 = wa-airpfrom ).
... wa-carrid ... wa-airpfrom ...
ENDLOOP.
wa
is reused in the group loop to access the group key.
- Group Key Binding when Grouping by One Column
GROUP BY wa-carrid
INTO DATA(key).
... key ...
ENDLOOP.
INTO
addition after GROUP BY
. Instead of reusing wa
,
an elementary data object key
is used here to represent the group. This can
be generated inline. The additions GROUP
SIZE, GROUP
INDEX, and WITHOUT
MEMBERS can only be used in the group key binding, which gives it more functions than the
representative binding. If these functions are not required, the representative binding can be used. The group key binding can also be used to make the use of the group key in the loop more explicit.
key
instead of wa
.
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
GROUP BY ( key1 = wa-carrid key2 = wa-airpfrom )
INTO DATA(key).
... key-key1 ... key-key2 ...
ENDLOOP.
key
is a structure with the components key1
and key2
. A member loop can be inserted in exactly the same way as when grouping by one column.
NO MEMBERS
to save time and memory.
GROUP BY ( key1 = wa-carrid key2 = wa-airpfrom
index = GROUP INDEX size = GROUP SIZE )
WITHOUT MEMBERS
INTO DATA(key).
... key-key1 ... key-key2 ... key-index ... key-size ...
ENDLOOP.
GROUP SIZE
.