简体   繁体   English

DELETE ADJACENT DUPLICATES 不删除重复项

[英]DELETE ADJACENT DUPLICATES does not delete duplicates

I have an internal table that is filled with 108 entries.我有一个包含 108 个条目的内部表。 From 9 to 9 entries it's repeating the entries and I wanted to delete those duplicates.从 9 到 9 个条目重复条目,我想删除这些重复项。 Since they're exactly the same I used the delete adjacent duplicates from itab comparing all fields .因为它们完全相同,所以我使用了delete adjacent duplicates from itab comparing all fields Also tried without comparing all fields .也尝试过不comparing all fields No success.没有成功。

If it helps, my table has 9 fields: bukrs, hkont, gjahr, belnr, budat, waers, shkzg, wrbtr, dmbtr and dmbe2.如果有帮助,我的表有 9 个字段:bukrs、hkont、gjahr、belnr、budat、waers、shkzg、wrbtr、dmbtr 和 dmbe2。 They're from BSIS and they're in this order too.他们来自 BSIS,他们也是按这个顺序排列的。 This is the DO loop in which is the SELECT enter code here .这是DO循环,其中是 SELECT enter code here I've putted the DELETE outside of the DO loop.我已将DELETE放在DO循环之外。

The two first SELECT 's are working fine and belong to the previous code that existed.前两个SELECT的工作正常,属于以前存在的代码。

DO 12 TIMES.
         lv_aux = lv_aux + 1.
         lv_tamanho = STRLEN( lv_aux ).
         IF lv_tamanho = 1.
           CONCATENATE '0' lv_aux INTO lv_aux.
         ENDIF.
         CONCATENATE p_gjahr lv_aux '01' INTO z_v_first_day.

         PERFORM get_last_day_of_month USING z_v_first_day
                                       CHANGING lv_last_day.

         " some other code irrelevant to the issue

         SELECT bukrs hkont gjahr belnr budat waers shkzg dmbtr wrbtr dmbe2 FROM bsis
           APPENDING CORRESPONDING FIELDS OF TABLE gt_bancbsis
           WHERE hkont = '0051100001'
           AND bukrs EQ p_bukrs
           AND budat <= lv_last_day.

         " some other code irrelevant to the issue
ENDDO.

DELETE ADJACENT DUPLICATES FROM gt_bancbsis COMPARING ALL FIELDS.

This is a picture of the internal table gt_bancbsis in the dubugger.这是调试器中的内部表 gt_bancbsis 的图片。调试器中的 itab

The word ADJACENT in the statement DELETE ADJACENT DUPLICATES is there for a very good reason: It states that only duplicate lines that are next to each other are removed.语句DELETE ADJACENT DUPLICATES中的单词ADJACENT有一个很好的理由:它声明只删除彼此相邻的重复行。 This is also stated in the online keyword documentation : 在线关键字文档中也说明了这一点:

Rows are considered to be duplicate if the content of neighboring row is the same in the components examined.如果检查的组件中相邻行的内容相同,则行被认为是重复的。 In the case of multiple duplicate rows following one another, all the rows (except for the first) are deleted.如果多个重复行彼此相邻,则删除所有行(第一行除外)。

This means: If your table contains the values这意味着:如果您的表包含值

 A B
 C D
 A B
 C D

it contains duplicate values, but since these are not adjacent, DELETE ADJACENT DUPLICATES will not remove them, no matter what you specify.它包含重复值,但由于这些值不相邻,因此无论您指定什么, DELETE ADJACENT DUPLICATES都不会删除它们。

The SELECT statement on the other hand does not guarantee a specific order when returning the datasets selected unless you tell it to ORDER BY one or more columns.另一方面, SELECT语句在返回所选数据集时不保证特定顺序,除非您告诉它按一个或多个列进行ORDER BY The rows are just returned in any order, and if the DELETE ADJACENT DUPLICATES statement works, it's pure coincidence.这些行只是以任何顺序返回,如果DELETE ADJACENT DUPLICATES语句有效,那纯属巧合。 It might even work on one system, stop working on another and remove only half of the duplicates on a third system.它甚至可能在一个系统上工作,在另一个系统上停止工作,并在第三个系统上只删除一半的重复项。 So, cardinal rule:所以,基本规则:

Make sure that your internal table is sorted by the fields you want to be checked for duplicates BEFORE deleting the duplicates.在删除重复项之前,请确保您的内部表按要检查重复项的字段排序。

For better scalability, you should use the SORT statement instead of having the database sort the rows with ORDER BY .为了更好的可伸缩性,您应该使用SORT语句而不是让数据库使用ORDER BY对行进行排序。 So you could use either所以你可以使用

SORT gt_bancbsis.
DELETE ADJACENT DUPLICATES FROM gt_bancbsis.

or, if you only want to check for certain fields,或者,如果您只想检查某些字段,

SORT gt_bancbsis BY foo bar baz.
DELETE ADJACENT DUPLICATES FROM gt_bancbsis COMPARING foo bar baz.

In BSIS table, you are using 4 jey fields( bukrs, hkont, gjahr, belnr ).BSIS表中,您使用了 4 个 jey 字段( bukrs、hkont、gjahr、belnr )。 Use these field only for sorting.仅将这些字段用于排序。

  1. Sort the internal table first.先对内表排序

     SORT ITAB WITH KEY ITAB-FIELDS.
  2. Then COMPARING fields.然后比较字段。

     DELETE ADJACENT DUPLICATES FROM ITAB

    It will work fine.它会工作正常。

I have an internal table that is filled with 108 entries.我有一个内部表,其中包含108个条目。 From 9 to 9 entries it's repeating the entries and I wanted to delete those duplicates.从9到9个条目,它在重复这些条目,我想删除那些重复的条目。 Since they're exactly the same I used the delete adjacent duplicates from itab comparing all fields .由于它们完全相同,因此我使用了delete adjacent duplicates from itab comparing all fields Also tried without comparing all fields .还尝试了不comparing all fields No success.没有成功

If it helps, my table has 9 fields: bukrs, hkont, gjahr, belnr, budat, waers, shkzg, wrbtr, dmbtr and dmbe2.如果有帮助,我的表有9个字段:bukrs,hkont,gjahr,belnr,budat,waers,shkzg,wrbtr,dmbtr和dmbe2。 They're from BSIS and they're in this order too.它们来自BSIS,而且也按此顺序排列。 This is the DO loop in which is the SELECT enter code here .这是DO循环,这里是SELECT enter code here I've putted the DELETE outside of the DO loop.我已将DELETE放在DO循环之外。

The two first SELECT 's are working fine and belong to the previous code that existed.前两个SELECT可以正常工作,并且属于先前存在的代码。

DO 12 TIMES.
         lv_aux = lv_aux + 1.
         lv_tamanho = STRLEN( lv_aux ).
         IF lv_tamanho = 1.
           CONCATENATE '0' lv_aux INTO lv_aux.
         ENDIF.
         CONCATENATE p_gjahr lv_aux '01' INTO z_v_first_day.

         PERFORM get_last_day_of_month USING z_v_first_day
                                       CHANGING lv_last_day.

         " some other code irrelevant to the issue

         SELECT bukrs hkont gjahr belnr budat waers shkzg dmbtr wrbtr dmbe2 FROM bsis
           APPENDING CORRESPONDING FIELDS OF TABLE gt_bancbsis
           WHERE hkont = '0051100001'
           AND bukrs EQ p_bukrs
           AND budat <= lv_last_day.

         " some other code irrelevant to the issue
ENDDO.

DELETE ADJACENT DUPLICATES FROM gt_bancbsis COMPARING ALL FIELDS.

This is a picture of the internal table gt_bancbsis in the dubugger.这是dubugger中内部表gt_bancbsis的图片。调试器中的itab

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

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