New ABAP Syntax

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

MaterialErnamAenam
A1USR1USR1
A2USR2USR1
A3USR1USR1
A4USR3USR2
A5USR1USR2
A6USR2USR2
A7USR1USR3

This syntax will read the data like this:

MaterialErnamAenamGroup
A1USR1USR11
A3USR1USR11
A5USR1USR22
A7USR1USR33
A2USR2USR14
A6USR2USR25
A4USR3USR26

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)

Follow by Email
Twitter
LinkedIn
LinkedIn
Share