ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Internal Tables →  Processing Statements for Internal Tables →  AT - itab → 

Control Level Processing with Totals

This example demonstrates the calculation of totals in control level processing in internal tables.

Source Code

REPORT demo_int_tables_at_2.

CLASS demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.
    DATA: sflight_tab TYPE SORTED TABLE OF sflight
                      WITH UNIQUE KEY carrid connid fldate,
          sflight_wa  LIKE LINE OF sflight_tab.

    DATA: BEGIN OF output_wa,
             fldate    TYPE sflight-fldate,
             seatsocc  TYPE sflight-seatsocc,
           END OF output_wa,
           output LIKE TABLE OF output_wa WITH EMPTY KEY.

    SELECT *
           FROM sflight
           INTO TABLE sflight_tab.

    LOOP AT sflight_tab INTO sflight_wa.
      AT NEW connid.
        cl_demo_output=>next_section( |{ sflight_wa-carrid } | &&
                                      |{ sflight_wa-connid }| ).
      ENDAT.
      MOVE-CORRESPONDING sflight_wa TO output_wa.
      APPEND output_wa TO output.
      AT END OF connid.
        DATA(out) = cl_demo_output=>new(
          )->write( output ).
        CLEAR output.
        SUM.
        out->write( |Sum: | &&
                    |{ sflight_wa-seatsocc }| ).
      ENDAT.
      AT END OF carrid.
        SUM.
        out->line(
          )->write( |Carrier Sum: | &&
                    |{ sflight_wa-seatsocc }|
          )->line( ).
      ENDAT.
      AT LAST.
        SUM.
        out->write( |Overall Sum: | &&
                    |{ sflight_wa-seatsocc }| ).
      ENDAT.
    ENDLOOP.
    out->display( ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

Description

Control level processing for creating output. At the end of line groups, the total of reserved places is calculated and issued.