Skip to content

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, Random Grouping with LOOP

This example demonstrates a construction of the group key that does not depend on the table rows.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA(out) = cl_demo_output=>new( ).

    DATA(rnd) = cl_abap_random_int=>create(
      seed = CONV i( sy-uzeit ) min = 1 max = 10 ).

    DATA members LIKE numbers.
    LOOP AT numbers INTO DATA(line)
         GROUP BY rnd->get_next( )
                  ASCENDING
                  ASSIGNING FIELD-SYMBOL(<group>).
      out->begin_section( |Group Key: { <group> }| ).
      members = VALUE #( FOR <wa> IN GROUP <group> ( <wa> ) ).
      out->write( members
        )->write( |Lines: { lines( members ) }|
        )->end_section( ).
    ENDLOOP.

    out->display( ).

Description

Grouping of an internal table numbers with group key binding. The group key of the group loop is constructed as a value of the type i, which is determined as a random number fully independent of the internal table. This casts the table rows into groups of approximately the same size.

In the group loop, the rows of each group are placed in an internal table members using a table comprehension. This table is the output.