1

我只是想问一下如何计算内部表中的重复项。我想这样做是为了让我计算每个客户并将其放入客户计数列。

Sales Employee          Customer    Customer Count
a                          1             2
a                          2             2
b                          3             3
b                          2             3
b                          4             3
c                          1             1
4

2 回答 2

4

正如 suncatcher 在他的评论中提到的那样,使用 sql 聚合比循环遍历内部表更有效。但是,如果在您的情况下这是不可能的,一种方法是使用该collect语句。collect当已经存在具有相同关键字段的行时,将条目添加到内部表并添加数字字段。创建一个内部表,其中有一个字段用于您的销售员工,另一个字段用于计数并循环遍历您的销售表,使用 collect 为每个销售更新您的计数表。

types: begin of t_count,
       employee type text10,
       count type i,
       end of t_count.

data: it_count type standard table of t_count,
      wa_count type t_count.

loop at it_sales into wa_sales.
    move: wa_sales-employee to wa_count-employee,
          1 to wa_count-count.

    collect wa_count into it_count.
endloop.

该示例假定您有一个表it_sales和一个工作区wa_sales,两者都带有一个字段employee。然后表it_count包含您的员工列表(按照他们在您的销售表中出现的顺序)以及他们在销售表中出现的次数。

于 2018-04-25T08:12:33.690 回答
1
FIELD-SYMBOLS : <lfs_sales> TYPE ty_sales.

假设 li_sales 是一个包含 Sales_employee、Customer 和 customer_count 列的内部表。最初,表条目如下所示。

Sales_employee Customer customer_count
a                 1             0
a                 2             0
b                 3             0
b                 2             0
b                 4             0
c                 1             0

我们需要计算重复的 sales_employee 计数并更新 customer_count 字段。我们可以使用 Dirik 建议的 collect 语句,也可以使用如下所示的 control break 语句。

使用 SUM 关键字的前提是每行将 customer_count 初始化为 1,以便根据相似的 sales_employee 汇总客户数量。

LOOP AT li_sales ASSIGNING <lfs_sales>.
     <lfs_sales>-customer_count = 1.
ENDLOOP.

现在条目如下所示。

Sales_employee Customer customer_count
a                 1             1
a                 2             1
b                 3             1
b                 2             1
b                 4             1
c                 1             1

以下代码确实更新了 customer_count 字段值。

LOOP AT li_sales INTO rec_sales.
AT END OF employee.
  SUM.
  MOVE-CORRESPONDING rec_sales TO rec_count.
  APPEND rec_count TO li_count.
  CLEAR rec_count.
ENDAT.
ENDLOOP.

SORT li_count BY employee.
LOOP AT li_sales ASSIGNING <lfs_sales>.
  CLEAR rec_count.
  READ TABLE li_count INTO rec_count
  WITH KEY employee = <lfs_sales>-employee
  BINARY SEARCH.
  IF sy-subrc IS INITIAL.
    <lfs_sales>-count = rec_count-count.
  ENDIF.
ENDLOOP.

现在内部表被分配了 customer_count,如下所示。

Sales_employee Customer customer_count
a                 1             2
a                 2             2
b                 3             3
b                 2             3
b                 4             3
c                 1             1
于 2018-05-10T12:40:20.660 回答