How to use LOOP AT GROUP BY statement in SAP ABAP

The Group by is used to group a set of records from the internal table. It is similar to the old programming Technik AT ……. ENDAT. If you are new to SAP ABAP, please skip this part as it is an Advanced Technik.

Please remember the following points for the Group by definition.

  • There is always an enclosed loop to read the contents of the group.

As shown in above figure, the group by is defined in the outer loop. Group contents are read in inner loop.

  • It is grouped based on the key components. It is accessed from the assigned field-symbol.
  • Use LOOP AT Group by for the inner loop.

The following code can be used when each line of record in the group must be processed.

LOOP AT lt_contact ASSIGNING FIELD-SYMBOL(<ls_con>) GROUP BY  ( key1 = <ls_con>-contact_id ).

WRITE:/ ‘CONTACT id:’, <ls_con>-contact_id.

         WRITE:/ ‘CONTACT Name:’, <ls_con>-contact_name.

LOOP AT GROUP <ls_con> ASSIGNING FIELD_SYMBOL(<ls_group_data>).

APPEND <ls_group_data> TO lt_group_contact.

         ENDLOOP.

ENDLOOP.

LOOP AT lt_group_contact ASSIGNING FIELD-SYMBOL(<ls_check_group>).

           WRITE:/ <ls_check_group>-contact_id.

ENDLOOP.

Group-Key Binding: –

  • When each line of record in the group need not be processed, then the Group-key binding is considered. It can be used to explain the results of the group. In combination with ‘WITHOUT MEMBERS’, each lines of record does not need to stored additionally.
  • It will have two assignments in the LOOP AT line.
  • The Group size and Group index can be used to extract additional details such as the size of the group, Index of the group.
  • The contents of the group are accessed through the key names defined in the Grouping at the LOOP AT line. It is accessed through the second field-symbol assignment and not through the first field-symbol assignment.
  • LOOP AT Group can only be used, when ‘WITHOUT MEMEBRS’ is not mentioned.

DATA lt_contact TYPE TABLE OF ztt_db_table2.

DATA lt_group_Contact TYPE TABLE OF ztt_db_table2.

SELECT *FROM ztt_db_table2 INTO TABLE lt_contact.

LOOP AT lt_contact ASSIGNING FIELD-SYMBOL(<ls_contact>) GROUP BY

                                                        ( cont_id_key1 = <ls_contact>-contact_id

                                                          cont_name_key2 = <ls_contact>-contact_name

                                                          size_of_group = GROUP SIZE

                                                          index_line_group = GROUP INDEX )

                            WITHOUT MEMBERS

                            ASSIGNING FIELD_SYMBOL(<ls_group>).

              WRITE:/ ‘Contact id:’, <ls_group>-cont_id_key1.

              WRITE:/ ‘Contact Name:’, <ls_group>-cont_name_key2.

              WRITE:/ ‘Size of the group:’, <ls_group>-size_of_group.

              WRITE:/ ‘Index of the group:’, <ls_group>-index_line_group.

END LOOP.

WRITE:/ ‘END’.

The following code is an example without ‘WITHOUT MEMBERS’

DATA lt_contact TYPE TABLE OF ztt_db_table2.

DATA lt_group_Contact TYPE TABLE OF ztt_db_table2.

SELECT *FROM ztt_db_table2 INTO TABLE lt_contact.

LOOP AT lt_contact ASSIGNING FIELD-SYMBOL(<ls_contact>) GROUP BY

                                                        ( cont_id_key1 = <ls_contact>-contact_id

                                                          cont_name_key2 = <ls_contact>-contact_name

                                                          size_of_group = GROUP SIZE

                                                          index_line_group = GROUP INDEX )

                            ASSIGNING FIELD_SYMBOL(<ls_group>).

              WRITE:/ ‘Contact id:’, <ls_group>-cont_id_key1.

              WRITE:/ ‘Contact Name:’, <ls_group>-cont_name_key2.

              WRITE:/ ‘Size of the group:’, <ls_group>-size_of_group.

              WRITE:/ ‘Index of the group:’, <ls_group>-index_line_group.

END LOOP.

WRITE:/ ‘END’.