简体   繁体   English

计算内部表中的重复项?

[英]Count duplicates in an internal table?

I just want to ask on how to count duplicates in an internal table.我只是想问如何计算内部表中的重复项。 I wanted to this in order for me to count per customer and put it into the Customer count column.我想这样做是为了让我计算每个客户并将其放入客户计数列中。

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

as suncatcher mentions in his comment, using sql aggregates is more efficient than looping through internal tables.正如 suncatcher 在他的评论中提到的,使用 sql 聚合比循环内部表更有效。 But if that is not possible in your case, one way would be to use the collect statement.但如果在您的情况下这是不可能的,一种方法是使用collect语句。 collect adds entries to an internal table and adds up numerical fields when a row with the same key fields already exists.当具有相同关键字段的行已经存在时, collect将条目添加到内部表并添加数字字段。 Create an internal table with a field for your sales employee, another field for the count and loop through your sales table, using collect to update your count table for each sale.为您的销售员工创建一个包含一个字段的内部表,另一个用于计数的字段,并循环遍历您的销售表,使用 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.

The example assumes you have a table it_sales , a work area wa_sales , both with a field employee .该示例假设您有一个表it_sales和一个工作区wa_sales ,两者都有一个外勤employee Table it_count then contains a list of your employees (in the order they appear in your sales table) and the number of times they appeared in the sales table.然后表it_count包含您的员工列表(按照他们在您的销售表中出现的顺序)以及他们在销售表中出现的次数。

FIELD-SYMBOLS : <lfs_sales> TYPE ty_sales.

Assuming li_sales is an internal table with columns Sales_employee, Customer and customer_count.假设 li_sales 是一个包含 Sales_employee、Customer 和 customer_count 列的内部表。 Initially the table entries are present as follows.最初的表条目如下所示。

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

We need to calculate the duplicate sales_employee count and update the customer_count field.我们需要计算重复的 sales_employee 计数并更新 customer_count 字段。 We can make use of collect statement as suggested by Dirik or make use of control break statements as shown below.我们可以按照 Dirik 的建议使用 collect 语句,或者使用如下所示的 control break 语句。

Prerequisite to make use of SUM keyword is to initialize the customer_count as 1 in each row so that it can sum up the customer count based on similar sales_employee.使用 SUM 关键字的前提是将每行中的 customer_count 初始化为 1,以便它可以根据相似的 sales_employee 汇总客户数。

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

Now the entries look as shown below.现在条目如下所示。

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

Following code does update the customer_count field value.以下代码会更新 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.

Now the internal table gets assigned with customer_count as below.现在内部表被分配了 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM