SAP NetWeaver AS ABAP Release 752, ©Copyright 2017 SAP AG. All rights reserved.
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, Grouping with LOOP in Packages
This example demonstrates a construction of the group key that does not depend on the row content.
Source Code
REPORT demo_loop_group_by_packages.
CLASS demo DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
main,
class_constructor.
PRIVATE SECTION.
CLASS-DATA
itab TYPE TABLE OF decfloat34 WITH EMPTY KEY.
ENDCLASS.
CLASS demo IMPLEMENTATION.
METHOD main.
DATA(n) = 10.
cl_demo_input=>request( EXPORTING text = `Package size`
CHANGING field = n ).
IF n <= 0.
RETURN.
ENDIF.
cl_demo_output=>begin_section( |Packages of { n }| ).
DATA group LIKE itab.
LOOP AT itab INTO DATA(wa)
GROUP BY ( sy-tabix - 1 ) DIV n + 1.
CLEAR group.
LOOP AT GROUP wa ASSIGNING FIELD-SYMBOL(<wa>).
group = VALUE #( BASE group ( <wa> ) ).
ENDLOOP.
cl_demo_output=>write( group ).
ENDLOOP.
cl_demo_output=>display( ).
ENDMETHOD.
METHOD class_constructor.
itab = VALUE #( FOR j = 1 UNTIL j > 100 ( 1 / j ) ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo=>main( ).
Description
Grouping of an internal table text with representative binding. The group key of the group loop is constructed as a value of the type i. This value is calculated from the row index of the current row in sy-tabix. This creates groups of rows with the same definable size.
In the group loop, the rows of each group are placed in an internal table group in a member loop using the value operator with the addition BASE. These rows are then also displayed. Here, the group is addressed using the work area wa of the original output behavior of the LOOP due to the representative binding.
group could also be filled by the evaluation of a table comprehension sing FOR ... IN GROUP, instead of in a member loop LOOP AT GROUP:
The executable example for grouping with FOR demonstrates how the entire group loop can be implemented using expressions.