Delete Adjacent Duplicate


DELETE ADJACENT DUPLICATE statement works logically on sorted standard table and sorted table with non-unique key. It compares the adjacent rows. If similar records are found based on the comparing fields then it deletes from the second records onward. The system keeps only the first record. Let us suppose we are having the following table with records.

Item
Quantity (KG)
Price (Rs)
Rice
2
100
Sugar
1
50
Rice
3
150
Tea
1
100
Sugar
2
120

We know that in standard table APPEND statement is used to enter data records. Now APPEND appends data to the last position of the standard table. It is not possible to enter in any other position. Now if we run the Delete adjacent duplicate command on the previous table then the data records will remain same. Nothing will be changed. Because the command will check every single adjacent line and found nothing to similar. Hence in this case we need to sort the table (standard table).

Item
Quantity (KG)
Price (Rs)
Rice
2
100
Rice
3
150
Sugar
1
50
Sugar
2
120
Tea
1
100

Now we have found the sorted structure of the data records. In this position if the command runs then it will find two similar records based on the comparison of the item field. After evaluating this command we shall have the following.

Item
Quantity (KG)
Price (Rs)
Rice
2
100
Sugar
1
50
Tea
1
100

The command will remove always the second record onward. It will keep only the first record. Here if we use Sorted table with non-unique key then after inserting the records we can find the sorted structure of the data records. That is why DELETE ADJACENT DUPLICATE command always run sorted standard table or sorted table with non-unique key properly.
The statement is as follows.
DELETE ADJACENT DUPLICATES FROM itab.

If we want to mention specific fields then it will be like this. 
DELETE ADJACENT DUPLICATES FROM it_ekpo COMPARING ebeln.

In this statement we are comparing EBELN (PO No.). If we don't mention this then all fields will come into the comparison. Then any change in any field will be compared and it will fall into the unique category. So comparing field or fields is very important for this to avoid logical error.

REPORT  zabap_gui.

* Declaring the local structure of internal table
TYPES:
      BEGIN OF ty_tab,
        item     TYPE char10,
        quantity TYPE i,
        price    TYPE i,
      END OF ty_tab.

* Declaring the Sorted internal table with non unique key
DATA:
      itab TYPE SORTED TABLE OF ty_tab WITH NON-UNIQUE KEY item,
      wtab TYPE ty_tab.

* Entering records to each field
wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80.

* Now one single row has been fulfilled with data
* Next inserting one single row data into the table
INSERT wtab INTO TABLE itab.

wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70.
INSERT wtab INTO TABLE itab.

wtab-item = 'Tea'. wtab-quantity = 1. wtab-price = 100.
INSERT wtab INTO TABLE itab.

wtab-item = 'Rice'. wtab-quantity = 2. wtab-price = 80.
INSERT wtab INTO TABLE itab.

wtab-item = 'Horlicks'. wtab-quantity = 1. wtab-price = 200.
INSERT wtab INTO TABLE itab.

wtab-item = 'Suger'. wtab-quantity = 2. wtab-price = 70.
INSERT wtab INTO TABLE itab.

WRITE:  /3 'Item',
        13 'Quantity(KG)',
        28 'Price(Rs)'.
WRITE / '=========================================='.
SKIP" Skipping one single line

DELETE ADJACENT DUPLICATES FROM itab.

LOOP AT itab INTO wtab.
  WRITE:  /3 wtab-item,
          12 wtab-quantity,
          25 wtab-price.
ENDLOOP.

SKIP.
WRITE '=========================================='.


Share this

Related Posts