简体   繁体   中英

New ABAP syntax instead of COLLECT

Currently, I have this coding, and it works perfectly fine:

      TYPES: BEGIN OF tty_ekpo,
               ebeln TYPE ebeln,
               ebelp TYPE ebelp,
             END OF tty_ekpo.
      DATA: lt_ekpo TYPE TABLE OF tty_ekpo,
            ls_ekpo LIKE LINE OF lt_ekpo.
      LOOP AT gt_lopdata ASSIGNING FIELD-SYMBOL(<fs_collect>).
        ls_ekpo-ebeln = <fs_collect>-ebeln.
        ls_ekpo-ebelp = <fs_collect>-ebelp.
        COLLECT ls_ekpo INTO lt_ekpo.
      ENDLOOP.

I want to do the same with the new syntax, is it possible?
If yes, how?

I use this one:

lt_ekpo = VALUE #( FOR GROUPS ebelnebelp OF <ls_collect> IN gt_lopdata
                   GROUP BY ( ebeln = <ls_collect>-ebeln
                              ebelp = <ls_collect>-ebelp )
                   ASCENDING WITHOUT MEMBERS ( ebelnebelp ) ).

Well, I disagree with the Joszsef variant, because addition WITHOUT MEMBERS produces all group key values, it does NOT sum numeric fields automatically alike COLLECT . You need additional actions for this.

Here is the enhanced Joszef's variant that fits your needs and collects NETWR field:

  TYPES: BEGIN OF ty_ekpo,
             ebeln TYPE ebeln,
             ebelp TYPE ebelp,
             netwr TYPE ekpo-netwr,
           END OF ty_ekpo,
           tty_ekpo TYPE STANDARD TABLE OF ty_ekpo WITH EMPTY KEY.
  DATA: it_ekpo TYPE SORTED TABLE OF ty_ekpo WITH UNIQUE KEY ebeln ebelp.

DATA(lt1_ekpo) = VALUE tty_ekpo(
                 FOR GROUPS <group_key> OF <g> IN it_ekpo GROUP BY ( ebeln = <g>-ebeln ebelp = <g>-ebelp )
                 LET coll_line = REDUCE #( INIT line TYPE ty_ekpo FOR <m> IN GROUP <group_key>
                                           NEXT line-ebeln = <m>-ebeln line-ebelp = <m>-ebelp line-netwr = line-netwr + <m>-netwr )
                                 IN ( coll_line ) ) .

Another flavor based on two REDUCEs:

DATA(lt2_ekpo) = REDUCE tty_ekpo( INIT cline = VALUE tty_ekpo( )
                 FOR GROUPS <group_key> OF <g> IN it_ekpo GROUP BY ( ebeln = <g>-ebeln ebelp = <g>-ebelp )
                 NEXT cline = VALUE #( BASE cline ( ebeln = <group_key>-ebeln ebelp = <group_key>-ebelp
                                                    netwr = REDUCE netwr( INIT val TYPE netwr
                                                                          FOR wa IN
                                                                          FILTER #( it_ekpo WHERE ebeln = <group_key>-ebeln AND ebelp = <group_key>-ebelp )
                                                                          NEXT val = val + wa-netwr ) ) ) ).

I see that in your original post you also do not make summarizing, you just collecting key fields into table. If this is what you need, Joszef snippet is OK. Just to notice that COLLECT does more than that.

To answer my own question the result will be something line this:

TYPES: BEGIN OF ty_ekpo,
         ebeln TYPE ebeln,
         ebelp TYPE ebelp,
         netwr TYPE ekpo-netwr,
         matnr type ekpo-matnr,
       END OF ty_ekpo,
       tty_ekpo TYPE STANDARD TABLE OF ty_ekpo WITH EMPTY KEY.


DATA: it_ekpo TYPE SORTED TABLE OF ty_ekpo WITH UNIQUE KEY ebeln ebelp.
DATA(lt1_ekpo) = VALUE tty_ekpo(
             FOR GROUPS <group_key> OF <g> IN it_ekpo GROUP BY ( ebeln = <g>-ebeln ebelp = <g>-ebelp )
             LET coll_line = REDUCE #( INIT line TYPE ty_ekpo FOR <m> IN GROUP <group_key>
                                       NEXT line-ebeln = <m>-ebeln 
                                            line-ebelp = <m>-ebelp 
                                            line-netwr = line-netwr + <m>-netwr
                                            line-matnr = <m>-matnr )
                             IN ( coll_line ) ) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM