从内部表中提取唯一值 [英] Extracting unique values from an internal table

查看:84
本文介绍了从内部表中提取唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从内部表的一列或多列中提取唯一值的最有效方法是什么?

What is the most efficient way to extract the unique values from a column or multiple columns of an internal table?

推荐答案

如果您具有 7.40 SP08 或更高版本,则只需使用内联语法填充目标表(无需LOOP GROUP BY):

If you have 7.40 SP08 or above you can simply use the inline syntax to populate the target table (no need for LOOP GROUP BY):

DATA: it_unique TYPE STANDARD TABLE OF fieldtype.
it_unique = VALUE #(
  FOR GROUPS value OF <line> IN it_itab
  GROUP BY <line>-field WITHOUT MEMBERS ( value ) ).

这适用于任何类型的目标表。

This works with any type of the target table.

对于旧版本,请使用:

DATA: it_unique TYPE HASHED TABLE OF fieldtype WITH UNIQUE KEY table_line.
LOOP AT it_itab ASSIGNING <line>.
  INSERT <line>-field INTO TABLE lt_unique.
ENDLOOP.

上面的方法也适用于排序表。尽管我不建议为此目的使用排序表,除非您确实确定结果中只会包含几行。

The above works with sorted tables as well. Although I do not recommend to use sorted tables for this purpose unless you are really sure that only a few lines will be in the result.

非零<$ c只需忽略 INSERT 中的$ c> sy-subrc 。无需进行两次键查找(一次用于存在性检查,一次用于插入)。

The non-zero sy-subrc of INSERT is simply ignored. No need to do the key lookup twice (once for existence check, once for insert).

如果目标必须是标准表,并且您拥有旧的ABAP堆栈,也可以使用

If the target must be a STANDARD TABLE and you have an old ABAP stack you can alternatively use

DATA: it_unique TYPE STANDARD TABLE OF fieldtype.
LOOP AT it_itab ASSIGNING <line>.
  READ TABLE lt_unique WITH TABLE KEY table_line = <line>-field
    TRANSPORTING NO FIELDS BINARY SEARCH.
  INSERT <line>-field INTO lt_unique INDEX sy-tabix.
ENDLOOP.

这提供了与已排序表相同的行为,但具有标准表。
这是否比SORT / DELETE ADJACENT DUPLICATES更有效,取决于Itab中重复项的数量。重复项越多,上述解决方案将越快,因为它避免了不必要的附加到目标表的行为。但另一方面,追加比插入要快。

This provides the same behavior as with a sorted table but with a standard table. Whether this is more efficient than SORT / DELETE ADJACENT DUPLICATES depends on the number of duplicate entries in itab. The more duplicate entries exist the faster will be the above solution because it avoids the unnecessary appends to the target table. But on the other side appends are faster than inserts.

这篇关于从内部表中提取唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆