Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Internal Tables →  Expressions and Functions for Internal Tables →  FOR - Table Iterations →  Examples of Grouping with FOR 

Internal Table, Grouping with FOR Using Column Values

This example demonstrates the construction of the group key using simple value assignments.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    initialize( ).

    DATA out TYPE REF TO if_demo_output.
    out = REDUCE #(
      INIT o = cl_demo_output=>new(
                 )->begin_section( `Flights`
                 )->write( flights
                 )->begin_section( `Grouping` )
      FOR GROUPS group OF flight IN flights
            GROUP BY
              ( carrier = flight-carrid cityfr = flight-cityfrom
                size = GROUP SIZE index = GROUP INDEX )
            ASCENDING
      LET members = VALUE spfli_tab(
                       FOR <flight> IN GROUP group ( <flight> ) ) IN
      NEXT o = o->begin_section(
                    |Group Key: { group-carrier }, { group-cityfr }|
               )->write(
                    |Group Size: {  group-size  }, | &&
                    |Group Index: { group-index }|
               )->write( members )->end_section( ) ).

    out->display( ).

Description

This example works in the same way as the corresponding executable example for LOOP AT ... GROUP BY, but uses the expression FOR GROUPS ... OF for a table reduction with REDUCE instead of the group loop. In both cases, the group key after GROUP BY is constructed in exactly the same way.

The result of the table reduction is a reference to an object of the class CL_DEMO_OUTPUT to which the results of the grouping are written. The group members are written to a local auxiliary table members using a table comprehension with VALUE. This table comprehension could also be used in the executable example for LOOP AT ... GROUP BY instead of the member loop.