본문 바로가기
ABAP

[New Syntax] COLLECT

by clode 2023. 3. 2.
728x90
반응형
  TYPES: BEGIN OF TY_EKBE, "collect int
           BUDAT(6),
           WERKS    TYPE EKBE-WERKS,
           MATNR    TYPE EKBE-MATNR,
           LIFNR    TYPE EKKO-LIFNR,
           WAERS    TYPE WAERS,
           MENGE    TYPE MENGE_D,
           WRBTR    TYPE WRBTR,
           DMBTR    TYPE DMBTR,
           NETPR    TYPE DMBTR,
         END OF TY_EKBE,
         TTY_EKBE TYPE STANDARD TABLE OF TY_EKBE WITH EMPTY KEY.


*-- 월별(BUDAT(6)/공급업체(LIFNR)/자재(MATNR)별로 수량과 금액 COLLECT
  TRY .
      DATA(LT_COLT) = VALUE TTY_EKBE(
                       FOR GROUPS <GROUP_KEY> OF <G> IN LT_ACT
                       GROUP BY ( BUDAT = <G>-BUDAT WERKS = <G>-WERKS MATNR
                       = <G>-MATNR LIFNR = <G>-LIFNR WAERS = <G>-WAERS )
                       LET COLL_LINE = REDUCE #( INIT LINE TYPE TY_EKBE FOR
                       <M> IN GROUP <GROUP_KEY>
                           NEXT LINE-WERKS = <M>-WERKS
                                LINE-BUDAT = <M>-BUDAT
                                LINE-MATNR = <M>-MATNR
                                LINE-LIFNR = <M>-LIFNR
                                LINE-WAERS = <M>-WAERS
                                LINE-MENGE = LINE-MENGE + <M>-MENGE
                                LINE-WRBTR = LINE-WRBTR + <M>-WRBTR
                                LINE-DMBTR = LINE-DMBTR + <M>-DMBTR
                                LINE-NETPR = LINE-WRBTR / LINE-MENGE   )
                             IN ( COLL_LINE ) ) .
    CATCH CX_ROOT INTO DATA(LX_EXC).
      DATA(LV_MSG) = LX_EXC->GET_TEXT( ).
  ENDTRY.



구글 예시

TYPES:
  BEGIN OF ts_sales_order_item,
    material      TYPE matnr,
    item_price    TYPE netwr_ap,
    item_quantity TYPE kwmeng,
  END OF ts_sales_order_item,
  tt_sales_order_item TYPE STANDARD TABLE OF ts_sales_order_item WITH DEFAULT KEY.

SELECT matnr  AS material,
       ntgew  AS item_price,
       kwmeng AS item_quantity
  FROM vbap
    UP TO 10 ROWS
    INTO TABLE @DATA(lt_data).

DATA(lt_sales_order_item) = VALUE tt_sales_order_item(
        FOR GROUPS <group_key> OF <group> IN lt_data GROUP BY ( material = <group>-material )
          LET coll_line = REDUCE #(
              INIT line TYPE ts_sales_order_item FOR <member> IN GROUP <group_key>
              NEXT line-material      = <member>-material
                   line-item_price    = ( line-item_price + <member>-item_price ) / 2
                   line-item_quantity = line-item_quantity + <member>-item_quantity )
          IN ( coll_line ) ) .

cl_demo_output=>write( lt_sales_order_item ).

cl_demo_output=>display( ).
728x90
반응형

'ABAP' 카테고리의 다른 글

[Field symbol] 가로데이터->세로로  (0) 2023.03.10
[New syntax] 정리  (0) 2023.03.02
문자열 자릿수만큼 split  (0) 2023.03.02
영문 포함하는 변수인지 확인하는법  (0) 2023.03.02
Dynamic Query  (0) 2023.03.02

댓글