2

目前,我有这个编码,它工作得很好:

      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.

我想对新语法做同样的事情,有可能吗?
如果是,如何?

4

3 回答 3

5

我用这个:

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 ) ).
于 2019-09-13T09:53:06.317 回答
2

好吧,我不同意 Joszsef 变体,因为加法WITHOUT MEMBERS会产生所有组键值,它不会自动对数字字段求和COLLECT。为此,您需要采取其他措施。

这是适合您需要并收集 NETWR 字段的增强型 Joszef 变体:

  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 ) ) .

基于两个 REDUCE 的另一种风味:

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 ) ) ) ).

我看到在您的原始帖子中您也没有进行总结,您只是将关键字段收集到表格中。如果这是您需要的,Joszef 片段就可以了。只是要注意它的COLLECT作用远不止于此。

于 2019-09-14T17:56:47.143 回答
0

要回答我自己的问题,结果将是这样的:

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 ) ) .
于 2021-01-14T09:18:18.733 回答