DELETE ADJACENT DUPLICATES delete order?

6.6k views Asked by At

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?

3

There are 3 answers

0
PATRY Guillaume On BEST ANSWER

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,

4
René On

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.
0
pmarra On

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