ise*_*aou 4 abap internal-tables
如何使用 FOR 循环根据另一个字段中的相同值添加一个字段中的值?
\nTypes:\nBegin of ty_final,\n doctype type char5,\n posnr type char5,\n total type quan5,\nEnd of ty_final.\n\nDATA(lt_final)\xc2\xa0=\xc2\xa0VALUE\xc2\xa0ty_final( \n FOR\xc2\xa0line\xc2\xa0IN\xc2\xa0lt_history\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 WHERE\xc2\xa0(\xc2\xa0hist_type\xc2\xa0=\xc2\xa0'U'\xc2\xa0)\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 (\xc2\xa0doctype\xc2\xa0=\xc2\xa0line-hist_type \n total\xc2\xa0 =\xc2\xa0line-quantity\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 posnr \xc2\xa0 =\xc2\xa0line-po_item \xc2\xa0)\xc2\xa0). \n
Run Code Online (Sandbox Code Playgroud)\n我在 LT_HISTORY 中有什么:
\nHIST_TYPE POSNR QUANTITY\n U 10 5\n U 10 2\n U 20 3\n U 20 -3\n
Run Code Online (Sandbox Code Playgroud)\n我在 LT_FINAL 中需要什么:
\nDOCTYPE POSNR QUANTITY\n U 10 7\n U 20 0\n
Run Code Online (Sandbox Code Playgroud)\n我试图用来获取基于和字段的字段GROUP BY
中的值的总和。只是我不确定我到底需要在循环中添加什么。让我头晕。所以我尝试尽可能简单。TOTAL
POSNR
DOCTYPE
GROUP BY
FOR
REDUCE
下面是最小的可重现示例,它使用 ABAP 单元(Ctrl+Shift+F10 来运行它)。我评论了你的代码并将其替换为解决方案:
CLASS ltc_test DEFINITION FOR TESTING
DURATION SHORT RISK LEVEL HARMLESS.
PRIVATE SECTION.
METHODS test FOR TESTING.
ENDCLASS.
CLASS ltc_test IMPLEMENTATION.
METHOD test.
TYPES: BEGIN OF ty_line,
doctype TYPE c LENGTH 1,
posnr TYPE i,
quantity TYPE i,
END OF ty_line,
ty_table TYPE STANDARD TABLE OF ty_line WITH DEFAULT KEY.
DATA(lt_history) = VALUE ty_table(
( doctype = 'U' posnr = 10 quantity = 5 )
( doctype = 'U' posnr = 10 quantity = 2 )
( doctype = 'U' posnr = 20 quantity = 3 )
( doctype = 'U' posnr = 20 quantity = -3 ) ).
DATA(lt_final) = VALUE ty_table(
* FOR line IN lt_history
* WHERE ( doctype = 'U' )
* ( doctype = line-doctype
* quantity = line-quantity
* posnr = line-posnr ) ).
FOR GROUPS grp_line OF line IN lt_history
WHERE ( doctype = 'U' )
GROUP BY ( doctype = line-doctype posnr = line-posnr )
( doctype = grp_line-doctype
quantity = REDUCE #( INIT t = 0 FOR <line> IN GROUP grp_line
NEXT t = t + <line>-quantity )
posnr = grp_line-posnr ) ).
cl_abap_unit_assert=>assert_equals( act = lt_final exp = VALUE ty_table(
( doctype = 'U' posnr = 10 quantity = 7 )
( doctype = 'U' posnr = 20 quantity = 0 ) ) ).
ENDMETHOD.
ENDCLASS.
Run Code Online (Sandbox Code Playgroud)