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

This example demonstrates nested control level processing using GROUP BY.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    TYPES: BEGIN OF group,
             fldate   TYPE sflight-fldate,
             seatsocc TYPE sflight-seatsocc,
           END OF group.

    DATA: sflight_tab TYPE SORTED TABLE OF sflight
                      WITH UNIQUE KEY carrid connid fldate,
          group       TYPE TABLE OF group WITH EMPTY KEY.


    DATA(display_members) = abap_false.
    cl_demo_input=>request( EXPORTING text  = `Display Group Members?`
                            CHANGING  field = display_members ).

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

    SELECT *
           FROM sflight
           INTO TABLE @sflight_tab.

    DATA(total_sum)  = 0.
    LOOP AT sflight_tab INTO DATA(carrid_group)
         GROUP BY carrid_group-carrid.
      DATA(carrid_sum) = 0.
      LOOP AT GROUP carrid_group INTO DATA(connid_group)
           GROUP BY connid_group-connid.
        out->next_section( |{ connid_group-carrid } | &&
                           |{ connid_group-connid }| ).
        CLEAR group.
        DATA(connid_sum) = 0.
        LOOP AT GROUP connid_group INTO DATA(connid_member).
          group = VALUE #( BASE group
                         ( CORRESPONDING #( connid_member ) ) ).
          connid_sum = connid_sum + connid_member-seatsocc.
        ENDLOOP.
        carrid_sum = carrid_sum + connid_sum.
        IF to_upper( display_members ) = abap_true.
          out->write( group ).
        ENDIF.
        out->write( |Sum: | &&
                    |{ connid_sum }| ).
      ENDLOOP.
      out->line(
        )->write( |Carrier Sum: | &&
                  |{ carrid_sum }|
        )->line( ).
      total_sum = total_sum + carrid_sum.
    ENDLOOP.
    out->write( |Overall Sum: | &&
                |{ total_sum }|
       )->display( ).

Description

This example works in the same way as the example for control level processing with nested groups. The member loop of the outer group loop is itself a group loop (using the addition GROUP BY) and contains a further member loop. The group keys of the outer and inner grouping are constructed by specifying the required columns explicitly and, unlike in control level processing, do not depend on the structure of the table and its sorting. The totals of the space used per group are calculated explicitly.

The example for grouping with FOR demonstrates how the nested group loops can be implemented using expressions.