Skip to Content
0
Jan 04, 2023 at 03:21 PM

ABAP: Selecting the first row of each group by the keys

466 Views

Hi Pros,

in such an ascending sorted table below with the first 4 fields from the left as keys, how could I select the first row of each group by the fields OI_EBELN and OI_EBELP as group keys?

Thank you and regards,

Novice L

sch.png

    TYPES: BEGIN OF ty_dso1,

             /bic/zoi_ebeln TYPE /bic/oizoi_ebeln,
             oi_ebelp       TYPE /bi0/oioi_ebelp,
             /bic/zlfdnr_3  TYPE /bic/oizlfdnr_3,
             /bic/zmm_chgnr TYPE /bic/oizmm_chgnr,
             /bic/zmm_chgin TYPE /bic/oizmm_chgin,
             /bic/zmm_aldtm TYPE /bic/oizmm_aldtm,

           END OF ty_dso1.

    DATA: it_tab1 TYPE SORTED TABLE OF ty_dso1
        WITH UNIQUE KEY /bic/zoi_ebeln oi_ebelp /bic/zlfdnr_3
        /bic/zmm_chgnr,
          wa_tab1 LIKE LINE OF it_tab1.

    IF NOT RESULT_PACKAGE IS INITIAL.
      CLEAR it_tab1.


        SELECT /bic/zoi_ebeln oi_ebelp /bic/zlfdnr_3 /bic/zmm_chgnr
        /bic/zmm_chgin /bic/zmm_aldtm
        UP TO 1 ROWS
        FROM /bic/ammawcdp011
        INTO TABLE it_tab1 FOR ALL ENTRIES IN RESULT_PACKAGE

        WHERE   /bic/zoi_ebeln = RESULT_PACKAGE-oi_ebeln AND
                oi_ebelp = RESULT_PACKAGE-oi_ebelp AND
               /bic/zmm_chgin = 'U'.

      
    ENDIF.


    CLEAR wa_tab1.
    LOOP AT RESULT_PACKAGE ASSIGNING <result_fields> WHERE
       /bic/zmm_chgin = 'U'.

      READ TABLE it_tab1 INTO wa_tab1 WITH KEY
      /bic/zoi_ebeln = <result_fields>-oi_ebeln
      oi_ebelp = <result_fields>-oi_ebelp BINARY SEARCH.

      IF sy-subrc = 0.

        <result_fields>-/bic/zmm_inldm = wa_tab1-/bic/zmm_aldtm.
      ENDIF.

    ENDLOOP.

Attachments

sch.png (61.3 kB)