Dustin Sun
Dustin Sun

Reputation: 5532

DELETE ADJACENT DUPLICATES delete order?

If there are entries with the same key.

sort itab by key. delete adjacent duplicates from itab comparing key.

Does anyone know which one will be deleted if delete adjacent duplicates..comparing key? The first one or second one?

Upvotes: 6

Views: 6925

Answers (3)

pmarra
pmarra

Reputation: 1

If data in your itab are fetched from DB, it's better than you use ORDER BY addition in SELECT and than you can use the delete adjacent duplicates . Sorting algorithm costs nlog(n) and is better that DBMS does these type of operation instead ABAP. Obviously that if you can do the DISTINCT or GROUP BY in SQL you avoid to use both SORT and delete adjacent duplicates and you should solve all performance problems

Upvotes: 0

René
René

Reputation: 2942

Instead of sorting a standard table, you could consider declaring another internal table as a sorted table of the same type with a unique key corresponding to the fields you're comparing to eliminate the duplicates. It's faster, allows you to keep your original table unchanged, and, in my opinion, makes your code more readable because it's easier to understand which rows are kept and which ones are not. Example:

LOOP AT itab ASSIGNING <itab_row>.
  INSERT <itab_row> INTO TABLE sorted_itab.
ENDLOOP.

Upvotes: 5

PATRY Guillaume
PATRY Guillaume

Reputation: 4327

From F1 help on "delete adjacent duplicate"

In the case of several double lines following one another, all the lines - except for the first - are deleted.

So the second (identical) line should be deleted

Regards,

Upvotes: 12

Related Questions