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. 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.
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. The rows are just returned in any order, and if the DELETE ADJACENT DUPLICATES
statement works, it's pure coincidence. 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
. 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.