查找每个唯一供应商的总借方值

时间:2018-06-18 04:40:15

标签: sap abap

我的内部表格如下:

Id     vendor  doc_no  debit  credit
Abc.  Ven1.    123.       4000. 5000
Abc.  Ven2.    345.       5000  6000
Abc.  Ven1.    367.       8000. 9000
Abc.  Ven2.    890.       3000. 8000

现在,我需要为每个供应商找到最大的借记。在上述示例的基础上,对于供应商 Ven1。,最大借记将为(4000+8000) = 12000。同样,对于 Ven2。供应商,最大借记值为(5000+3000) = 8000

如何实现这个?

3 个答案:

答案 0 :(得分:0)

使用正确的维度和度量顺序创建包含所需字段的结构。然后你可以节省使用" AT"功能,汇总每个""组的借记卡。供应商。

DATA: Begin of ls_itab_ordered
 vendor type vendor, 
 debit type debit,
 End of ls_itab_ordered,
lt_itab_ordered like standard table of ls_itab_ordered,
lt_itab_agg like itab.

move-corresponding itab to lt_itab_ordered
sort lt_itab_ordered by vendor.

loop at lt_itab_ordered assigning <ls_itab_ordered>.
  at new vendor.
    ls_itab_agg-vendor = <ls_itab_ordered>-vendor.
  endat.
  add <ls_itab_ordered>-debit to ls_itab_agg-debit.
  at end of vendor.
    append ls_itab_agg to lt_itab_agg.
    clear ls_itab_agg.
  endat.
endloop.

答案 1 :(得分:0)

您可以使用Collect -

我认为it_vendor是您的内部表格,并声明ls_vendor类似it_vendor

TYPES: BEGIN OF ty_calc,
        vendor TYPE vendor, " (check DE of vendor then use that)
        debit TYPE int4,
      END OF ty_calc.

DATA: ls_calc TYPE ty_calc, 
      lt_calc TYPE TABLE OF ty_calc.

LOOP AT it_vendor INTO ls_vendor.
  ls_calc-vendor = ls_vendor-vendor.
  ls_calc-debit  = ls_vendor-debit.

  COLLECT ls_calc INTO lt_calc.
  CLEAR : ls_calc , ls_vendor.
ENDLOOP.

内部表lt_calc拥有每个供应商的最大借记。

答案 2 :(得分:0)

这是一个示例报告,其中包含我能想到的最高性能解决方案。它使用了相当新的ABAP命令FOR GROUPS … INREDUCE。只需将其复制到您的系统中并尝试一下。

REPORT Z_SUM_BY_VENDOR.

"Prepare test data
TYPES: BEGIN OF lty_st_data_source,
         id     TYPE C LENGTH 3,
         vendor TYPE C LENGTH 4,
         doc_no TYPE C LENGTH 3,
         debit  TYPE I,
         credit TYPE I,
       END OF lty_st_data_source.

DATA: lt_data_source TYPE STANDARD TABLE OF lty_st_data_source.

APPEND VALUE #( id = 'Abc' vendor = 'Ven1' doc_no = '123' debit = 4000 credit = 5000 ) TO lt_data_source.
APPEND VALUE #( id = 'Abc' vendor = 'Ven2' doc_no = '345' debit = 5000 credit = 6000 ) TO lt_data_source.
APPEND VALUE #( id = 'Abc' vendor = 'Ven1' doc_no = '367' debit = 8000 credit = 9000 ) TO lt_data_source.
APPEND VALUE #( id = 'Abc' vendor = 'Ven2' doc_no = '890' debit = 3000 credit = 8000 ) TO lt_data_source.

" Define type for sum table and generate sum (actual answer to the question)
TYPES: BEGIN OF lty_st_grouped,
         vendor    TYPE C LENGTH 4,
         debit_sum TYPE I,
       END OF lty_st_grouped,
       lty_tt_grouped TYPE HASHED TABLE OF lty_st_grouped WITH UNIQUE KEY vendor.

DATA(lt_grouped) = VALUE lty_tt_grouped(
                     FOR GROUPS vendor_group OF <ls_data_source> IN lt_data_Source
                     GROUP BY <ls_data_source>-vendor
                     ( VALUE lty_st_grouped( vendor = vendor_group
                                             debit_sum = REDUCE i( INIT sum = 0
                                                                   FOR <ls_data_source_2> IN GROUP vendor_group
                                                                   NEXT sum = sum + <ls_data_source_2>-debit )
                                           )
                     )
                   ).

" Show result for demo purpose
LOOP AT lt_grouped ASSIGNING FIELD-SYMBOL(<ls_grouped>).
  WRITE: `Vendor: ` && <ls_grouped>-vendor && ` | Debit Sum: ` && <ls_grouped>-debit_sum.
  NEW-LINE.
ENDLOOP.