Group By Syntax for Internal table
The world is changing and ABAP is changing with it. Let’s see how the group by syntax work for internal tables work in ABAP.
SELECT *
UP TO 500 ROWS
FROM mara
INTO TABLE @DATA(it_mara).
LOOP AT it_mara INTO DATA(ls_mara)
GROUP BY ( key1 = ls_mara-ernam key2 = ls_mara-aenam )
ASSIGNING FIELD-SYMBOL(<group>).
LOOP AT GROUP <group> ASSIGNING FIELD-SYMBOL(<row>).
* Add your logic here
ENDLOOP.
ENDLOOP.
In this example, suppose there are following data set
Material | Ernam | Aenam |
A1 | USR1 | USR1 |
A2 | USR2 | USR1 |
A3 | USR1 | USR1 |
A4 | USR3 | USR2 |
A5 | USR1 | USR2 |
A6 | USR2 | USR2 |
A7 | USR1 | USR3 |
This syntax will read the data like this:
Material | Ernam | Aenam | Group |
A1 | USR1 | USR1 | 1 |
A3 | USR1 | USR1 | 1 |
A5 | USR1 | USR2 | 2 |
A7 | USR1 | USR3 | 3 |
A2 | USR2 | USR1 | 4 |
A6 | USR2 | USR2 | 5 |
A4 | USR3 | USR2 | 6 |
Field symbol “Group” carries the key combination and changes only for group. The item “Row” inherits the structure of MARA (from IT_MARA). If you don’t use group, you may need to use at end of or parallel cursor or some other logic. Now you have a new syntax.
Please note, you cannot change the driver table (in this case it_mara) here. If you want to change it, you need to use the addition – without members.