总结sap abap中的行

时间:2014-05-14 11:12:48

标签: sap abap collect

在sap我有一个表,有些名称相同但数量不同的行。 我想总结一下这样的行:

    SELECT c~aufnr
           p~matnr p~bdter p~meins p~baugr p~dbskz p~erfmg p~aufnr
           f~maktx
       INTO CORRESPONDING FIELDS OF TABLE it_tab
       FROM  afpo AS c
         INNER JOIN resb AS p ON c~aufnr = p~aufnr
         INNER JOIN makt AS f ON p~matnr = f~matnr.             

    LOOP AT it_tab INTO fs_tab.
       COLLECT fs_tab INTO it_tab_collected.
    ENDLOOP.
    it_tab = it_tab_collected.

但在这种情况下,它只能将绝对相同的行相加。我需要用相同的名称来总结行。

我怎样才能做到这一点?

此致 亚历山大。

3 个答案:

答案 0 :(得分:4)

正如icbytes所说,COLLECT使用关键字段来确定要聚合的字段。我建议定义一些数据类型以匹配您的情节:

TYPES: BEGIN OF t_my_type,
         key_a TYPE foo,
         key_b TYPE foo,
         nokey_c TYPE foo,
         nokey_d TYPE foo,
       END OF t_my_type,
       tt_my_type_list TYPE STANDARD TABLE OF t_my_type WITH DEFAULT KEY,
       tt_my_type_hash TYPE HASHED TABLE OF t_my_type WITH KEY key_a key_b.

DATA: lt_result TYPE tt_my_type_list,
      lt_sums   TYPE tt_my_type_hash.

FIELD-SYMBOLS: <ls_result> TYPE t_my_type.

LOOP AT lt_result ASSIGNING <ls_result>.
  COLLECT <ls_result> INTO lt_sums.
ENDLOOP.

...或者你可能想首先使用aggregate function ......

答案 1 :(得分:2)

COLLECT坚持,即使您可以保存几行代码,也无需使用它,无缘无故地添加限制

我在使用COLLECT之前编写的代码维护/添加功能方面遇到了问题,因为它不允许将非键非数字字段添加到内部表格,有时候我被迫修改了很多COLLECT个句子,或者给代码带来了不必要的复杂性(比如使用额外的内部表格)。

示例(以vwegert的示例为基础,谁能很好地解释如何正确使用COLLECT):

TYPES: BEGIN OF t_my_type,
         key_a TYPE foo,
         key_b TYPE foo,
         """ with COLLECT all 'foo' types **has to be** numeric
         nokey_c TYPE foo,
         nokey_d TYPE foo,
         """ if you ever need to add something like a note field to the table
         """ you'll find out that you have to change all the COLLECT sentences
         """ because this new field wouldn't let the code compile
         nokey_notes(50) type c,
       END OF t_my_type,

       tt_my_type_list TYPE STANDARD TABLE OF t_my_type WITH DEFAULT KEY,
       tt_my_type_hash TYPE HASHED TABLE OF t_my_type WITH KEY key_a key_b.

DATA: lt_result TYPE tt_my_type_list,
      lt_sums   TYPE tt_my_type_hash.

FIELD-SYMBOLS: <ls_result> TYPE t_my_type.


""" just imagine you need the 'nokey_notes' field for an ALV for user input.
PERFORM show_alv USING lt_result.


""" and you don't care about this new field when doing
""" the sum/average or whatever you are trying to calculate
""" well, it won't work with COLLECT...
LOOP AT lt_result ASSIGNING <ls_result>.
  COLLECT <ls_result> INTO lt_sums.
ENDLOOP.

我的建议将使用AT... ENDAT(除了SELECT的聚合函数,这是一个不错的选择,特别是如果您不需要单个数据)。

当然代码会获得一些额外的代码,但在我看来/经验中它们是值得的,因为代码将来会更容易维护。

TYPES: BEGIN OF t_my_type,
         key_a TYPE foo,
         key_b TYPE foo,
         """ with COLLECT all 'foo' types **has to be** numeric
         nokey_c TYPE foo,
         nokey_d TYPE foo,
         """ without COLLECT you are able to add new non-key non-numeric fields
         nokey_notes(50) type c,
       END OF t_my_type,

       tt_my_type_list TYPE STANDARD TABLE OF t_my_type.

DATA: lt_result TYPE tt_my_type_list,
      lt_sum    TYPE tt_my_type_list.
      lwa_sum   TYPE t_my_type.

FIELD-SYMBOLS: <ls_result> TYPE t_my_type.


""" just imagine you need the 'nokey_notes' field for an ALV for user input.
PERFORM show_alv USING lt_result.


""" sorting is important when using AT... ENDAT, there are other gotchas too
""" make sure you read its documentation carefully if you never used it
""" (like everything right? :P)
SORT lt_result BY key_a key_b.
REFRESH lt_sum.

""" and you don't care about 'nokey_notes' field when doing calculation
LOOP AT lt_result ASSIGNING <ls_result>.

  AT NEW key_b.
    """ this get executed when the work area's primary keys
    """ change, good time to prepare the lwa_sum work area.
    CLEAR lwa_sum.
    lwa_sum-key_a = lwa_sum-key_a.
    lwa_sum-key_b = lwa_sum-key_b.
  ENDAT.

  """ do whatever math/logic is need with the fields
  lwa_sum-nokey_c = lwa_sum-nokey_c + <ls_result>-nokey_c.
  lwa_sum-nokey_d = lwa_sum-nokey_d + <ls_result>-nokey_d.

  AT END OF key_b.
    """ this get executed when the work area's primary keys
    """ is about to change (in next iteration) or at the last
    """ record of the table
    """ good place to save the results to a new internal table
    APPEND lwa_sum to lt_sums.
  ENDAT.

ENDLOOP.

答案 2 :(得分:0)

AFAIK collect使用密钥,因为它可以决定哪些行将创建聚合。 如果您有其他字符值前置数字值,请在另一个itab中删除它们,以便填充的唯一类似C的列将是“名称”。这将成为让abap处理器聚合的唯一关键。

这有帮助吗?