GROUP BY语法_ABAP

GROUP BY语法_ABAP_02

GROUP BY语法_ABAP_03

TYPES:
  BEGIN OF typ_tab,
    col0 type char1,
    col1 type char1,
    col2 type i,
  END OF typ_tab,
  typ_t_tab TYPE STANDARD TABLE OF typ_tab WITH EMPTY KEY.

  DATA w_sum type i.
  DATA(lt_tab) = VALUE typ_t_tab( ( col0 = 'X' col1 = 'A' col2 = 1 )
                                  ( col0 = 'X' col1 = 'B' col2 = 2 )
                                  ( col0 = 'X' col1 = 'C' col2 = 3 )
                                  ( col0 = 'X' col1 = 'A' col2 = 4 )
                                  ( col0 = 'X' col1 = 'B' col2 = 5 )
                                  ( col0 = 'Z' col1 = 'B' col2 = 6 )
                                  ( col0 = 'Z' col1 = 'C' col2 = 7 )
                                  ( col0 = 'Z' col1 = 'C' col2 = 8 )
                                  ( col0 = 'Z' col1 = 'A' col2 = 9 ) ).

  LOOP AT lt_tab INTO DATA(wa_tab) GROUP BY ( key1 = wa_tab-col1 )
                 ASSIGNING FIELD-SYMBOL(<fs_tab>).

    WRITE: / <fs_tab>-key1, ' list:'.
    CLEAR w_sum.

    LOOP AT GROUP <fs_tab> INTO DATA(wa_group).
      w_sum = w_sum + wa_group-col2.
      WRITE / wa_group-col2.
    ENDLOOP.

    WRITE: / sy-uline,
           / 'Sum: ', w_sum.
  ENDLOOP.