Copyright 2024 - BV TallVision IT

There is a statement which will help you get rid of duplicate entries from an internal table - and it may be good to know which entry is actually removed, the original one or the actual duplicate ? It's the duplicate of course, however, you do have control... 

The sequence of the internal table plays an important role here. This is a simple key plus value example on the use of DELETE ADJACENT DUPLICATES FROM:

types: begin of ty_key_plus_value,
         key type c length 5,
         value type c length 5,
       end of ty_key_plus_value.
data: lt_key_plus_values type standard table of ty_key_plus_value,
      lw_key_plus_value type ty_key_plus_value.

lw_key_plus_value-key = 'ABC'.
lw_key_plus_value-value = 'VAL1'.
append lw_key_plus_value to lt_key_plus_values.
lw_key_plus_value-value = 'VAL2'.
append lw_key_plus_value to lt_key_plus_values.
lw_key_plus_value-value = 'VAL3'.
append lw_key_plus_value to lt_key_plus_values.

SORT lt_key_plus_values.
DELETE ADJACENT DUPLICATES FROM lt_key_plus_values
  COMPARING key.

The outcome of this exercise is an internal table with only 1 entry: Key="ABC" and value="VAL1".

So how can this be controlled ? Sort the table the other way around:

SORT lt_key_plus_values by key value descending.
DELETE ADJACENT DUPLICATES FROM lt_key_plus_values
  COMPARING key. 

The outcome of this would be an internal table with only 1 entry: Key="ABC" and value="VAL3".