Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Issue with Delete Adjacent Duplicate

Former Member
0 Likes
1,078

Hi Experts,

We use DELETE ADJACENT DUPLICATE to delete duplicate data in internal table as below:

SORT itab BY object_id.

DELETE ADJACENT DUPLICATE FROM itab COMPARING object_id.

My issue is how can we get the deleted duplicate and write it out to inform user that this object_id is actually duplicate?

Anyone has idea?

Thank you so much.

Regards,

Alexender Honda

1 ACCEPTED SOLUTION
Read only

RaymondGiuseppi
Active Contributor
0 Likes
1,011

Try a code like

SORT itab BY object_id.
LOOP AT itab INTO record.
  
AT NEW object_id.
     new_key
= abap_true.
    
CLEAR duplicate_key.
  
ENDAT.
  
IF new_key IS NOT INITIAL." first value of a key
     previous_record
= record.
    
CLEAR new_key.
  
ELSE." duplicate value of the key
    
IF duplicate_key IS INITIAL. " first duplicate key
       duplicate_key
= abap_true.
      
APPEND previous_record TO duplicate_itab.
    
ENDIF.
    
APPEND record TO duplicate_itab.
    
DELETE itab.
  
ENDIF.
ENDLOOP.

Regards,

Raymond

8 REPLIES 8
Read only

former_member585060
Active Contributor
0 Likes
1,011

Hi,

     I think might have to add an extra loop on that itab after SORT, Move to temp itab or write the duplicate records, then use the delete statement.

DATA : v_flag TYPE c LENGTH 1.

SORT itab BY object_id.

LOOP AT itab INTO wa_itab.

AT NEW object_id.

v_flag = 'X'.    " To skip this records from deletion

ENDAT.

IF v_flag = ' '.   " If v_flag is blank, means not the first records so it should be deleted.

* Write the records or move to temp table and use that table to display

ENDIF.

CLEAR : v_flag, wa_itab.

ENDLOOP.

DELETE ADJACENT DUPLICATE FROM itab COMPARING object_id.

Thanks & Regards

Bala Krishna

Read only

0 Likes
1,011

Hi Bala,

Below is how my itab look like:

Object_ID      Object_Type

    1                        A

    1                        B

    1                        C

    3                        A

    2                        D

By using your method the output i get the records is:

Object_ID      Object_Type

    1                        C

    1                        B

I need something which is when there is duplicate data for object_id as above which is 3 records are duplicate. I will need to write all the three duplicate records out as below:

Object_ID      Object_Type

    1                        C

    1                        B

     1                        A

Can this be achieve? The reason is i will need to let user know there are 3 records of objects_id are duplicate and let user to correct the data accordingly.

Thank you.

Regards,

Alexender

Read only

0 Likes
1,011

Hi,

    Try below code

DATA : lwa_itab  TYPE ty_itab,
           lwa_itab1 TYPE ty_itab.

DATA : lv_flag  TYPE c LENGTH 1,
           lv_tabix TYPE sy-tabix.


LOOP AT itab INTO lwa_itab.

lv_tabix = sy-tabix + 1.

wa_itab = lwa_itab.

AT NEW object_id.

READ TABLE itab INTO lwa_itab1 INDEX lv_tabix.
IF sy-subrc = 0.

IF lwa_itab-object_id = lwa_itab1-object_id.

  v_flag = 'X'.

ENDIF.

ENDAT.

IF v_flag = 'X'.   " If v_flag is X, means this Object_id has duplicate entries.

* Write the records or move to temp table and use that table to display

ENDIF.


CLEAR : v_flag, wa_itab, lwa_itab1, lwa_itab, lv_tabix.

ENDLOOP.

But after the Delete statement, all the records will be deleted, your report must be interactive....

Thanks & Regards

Bala Krishna

Read only

Private_Member_49934
Product and Topic Expert
Product and Topic Expert
0 Likes
1,011

Try someting like below. There are other options. One of them is below

Assumption : object_id should be the first field of internal table for at event to work properly.

sort itab by object_id.

data : l_v_cntr type i.

i = 1.

loop at itab into wa.

at end of object_id.

if l_v_cntr > 1.

write : 'There are ' , l_v_cntr , ' duplicate records for ' , object_id.

endif.

l_v_cntr =  1.

endat.

 

endloop.

Read only

Former Member
0 Likes
1,011

what can be done..

data : lv_count type i.

SORT itab BY object_id.

LOOP AT ITAB.

add 1 to lv_count.

AT END OF OBJECT_ID.

if lv_count > 1.

* its a duplicate case

endif.

lv_count = 0.

ENDAT.

ENDLOOP.

DELETE ADJACENT DUPLICATE FROM itab COMPARING object_id.

Regards

Purna

Read only

Former Member
0 Likes
1,011

Find out duplicate record and display that duplicate record after that u delete duplicate record.

     

DATA: BEGIN OF connection,
         cityfrom TYPE spfli-cityfrom,
         cityto   TYPE spfli-cityto,
         distid   TYPE spfli-distid,
         distance TYPE spfli-distance,
       END OF connection.
data : lv_count type i.
DATA connection_tab LIKE connection OCCURS 0 WITH HEADER LINE.
DATA :it_dup LIKE connection OCCURS 0 WITH HEADER LINE.
BREAK DEVELOPER.

SELECT cityfrom cityto distid distance

        FROM spfli

        INTO TABLE connection_tab.
SORT connection_tab BY cityfrom cityto.
it_dup[] = connection_tab[].
*DELETE ADJACENT DUPLICATES FROM connection_tab COMPARING ALL FIELDS.
LOOP AT connection_tab.
   add 1 to lv_count.
   AT END OF cityfrom.
     if lv_count > 1.

     WRITE / connection_tab-cityfrom.
     WRITE / connection_tab-cityto.
     ENDIF.
     lv_count = 0.
ENDAT.
ENDLOOP.
DELETE ADJACENT DUPLICATES FROM connection_tab COMPARING ALL FIELDS.

Read only

Former Member
0 Likes
1,011

also Check another following example which display duplicate entries .

TABLES : BORIDENT.
TYPES: BEGIN OF ty_itab,
          objid  type   borident-objkey,
          objtype type  borident-OBJTYPE,
        END OF ty_itab .

DATA : lwa_itab  TYPE ty_itab,
            lwa_itab1 TYPE ty_itab.

DATA : lv_flag  TYPE c LENGTH 1,
            lv_tabix TYPE sy-tabix.
DATA: it_data   TYPE TABLE OF ty_itab,
       it_out    TYPE TABLE OF ty_itab,
       iw_out    TYPE ty_itab,
       w_sum_kun TYPE i,
       iw_data   TYPE ty_itab.
data v_flag(1) type c.

iw_data-objid = '1'.
iw_data-objtype = 'a'.
APPEND iw_data TO it_data.

iw_data-objid = '1'.
iw_data-objtype = 'b'.
APPEND iw_data TO it_data.

iw_data-objid = '1'.
iw_data-objtype = 'c'.
APPEND iw_data TO it_data.

iw_data-objid = '2'.
iw_data-objtype = 'a'.
APPEND iw_data TO it_data.

iw_data-objid = '2'.
iw_data-objtype = 'b'.
APPEND iw_data TO it_data.

iw_data-objid = '2'.
iw_data-objtype = 'c'.
APPEND iw_data TO it_data.

iw_data-objid = '3'.
iw_data-objtype = 'a'.
APPEND iw_data TO it_data.

iw_data-objid = '3'.
iw_data-objtype = 'b'.
APPEND iw_data TO it_data.
break developer.
data : lv_count type i.
LOOP AT it_data INTO lwa_itab.
add 1 to lv_count.
if lv_count > 1.
   WRITE :/ lwa_itab-objid.
      WRITE :/ lwa_itab-objtype.
      endif.
AT end of objid.

        lv_count = 0.
ENDAT.

at last.
   lv_count = 0.
   endat.
ENDLOOP.

DELETE ADJACENT DUPLICATES FROM it_data COMPARING ALL FIELDS.

Read only

RaymondGiuseppi
Active Contributor
0 Likes
1,012

Try a code like

SORT itab BY object_id.
LOOP AT itab INTO record.
  
AT NEW object_id.
     new_key
= abap_true.
    
CLEAR duplicate_key.
  
ENDAT.
  
IF new_key IS NOT INITIAL." first value of a key
     previous_record
= record.
    
CLEAR new_key.
  
ELSE." duplicate value of the key
    
IF duplicate_key IS INITIAL. " first duplicate key
       duplicate_key
= abap_true.
      
APPEND previous_record TO duplicate_itab.
    
ENDIF.
    
APPEND record TO duplicate_itab.
    
DELETE itab.
  
ENDIF.
ENDLOOP.

Regards,

Raymond