Application Development 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: 

Delete duplicate from internal table

Former Member
0 Kudos

HI Abapers,

I have a query on how to remove the duplicates from an internal table

My internal table data is as follows :

Cno Catg1 Catg2

01 0 1000

01 2000 0

I want to get only one record as

01 2000 1000

How to get the result.

I tried sorted by cno and used delete duplicates but it was not helpful.

Is there any other alternative to get this done

Please help me.

Regards,

Priya

1 ACCEPTED SOLUTION

Former Member

check it out with delete adjacent duplicate records

Deleting Adjacent Duplicate Entries

To delete adjacent duplicate entries use the following statement:

DELETE ADJACENT DUPLICATE ENTRIES FROM <itab>

[COMPARING <f1> <f 2> ...

|ALL FIELDS].

The system deletes all adjacent duplicate entries from the internal table <itab>. Entries are duplicate if they fulfill one of the following compare criteria:

Without the COMPARING addition, the contents of the key fields of the table must be identical in both lines.

If you use the addition COMPARING <f1> <f 2> ... the contents of the specified fields <f 1 > <f 2 > ... must be identical in both lines. You can also specify a field <f i > dynamically as the contents of a field <n i > in the form (<n i >). If <n i > is empty when the statement is executed, it is ignored. You can restrict the search to partial fields by specifying offset and length.

If you use the addition COMPARING ALL FIELDS the contents of all fields of both lines must be identical.

You can use this statement to delete all duplicate entries from an internal table if the table is sorted by the specified compare criterion.

If at least one line is deleted, the system sets SY-SUBRC to 0, otherwise to 4.

9 REPLIES 9

Former Member
0 Kudos

Hi Priya,

You Can use Collect Statement.

COLLECT is used to Summarize the Data in internal table while adding the rows.

Collect <wa> into <itab>.

This statement compares the Non-numeric(Type C,N,D,T,X,String) fields of the work area with the Existing rows in the internal table. that means all the Non-numeric fields will act as key (For Eg Matno, Plant)

If a row is found with the same key:

It will add the Numeric fields instead of creating a new row.

If a row is not found with the same key:

It will create a new row like Append.

DATA : BEGIN OF ITAB1 OCCURS 0,

MATNR TYPE MARD-MATNR,

WERKS TYPE MARD-WERKS,

LABST TYPE MARD-LABST,

END OF ITAB.

DATA :WA LIKE ITAB1.

DATA: ITAB2 LIKE ITAB1 OCCURS 0.

SELECT MATNR WERKS LABST FROM MARD INTO TABLE ITAB1.

LOOP AT ITAB1 INTO WA.

COLLECT WA INTO ITAB2.

ENDLOOP.

Check the contents of both ITAB1 AND ITAB2.

Thanks.

Former Member
0 Kudos

sort itab by cno catg1 descending.

delete adjacent duplicates from itab comparing cno.

Regards

Vasu

former_member200338
Active Contributor
0 Kudos

Hi,

try to sort the internal table based on the three field.

Like this.

Soirt it_tab1 by cno catg1 catg2.

DELETE ADJACENT DUPLICATES FROM lt_tab1 comparing cno catg1 catg2.

or try this logic.

read table it_tab1 into ls_tab1_temp index 1.

l_count = 0.

loop at it_tab1 into ls_tab1.

if ls_tab1-cno eq ls_temp_temp-cno. <<add the conditions>>

l_count = l_count + 1.

endif.

if l_count GT 1.

delete it_tab1 from ls_tab1_temp index sy-tabix.

clear l_count.

endif.

ls_tab1_temp = ls_tab1.

endloop.

Regards,

Niyaz

former_member404244
Active Contributor
0 Kudos

hi,

try like this

Loop at itab into watab.

read table itab into watab1 where cno = watab-cno.

if sy-subrc eq 0.

move : watab1-cno to watab2-cno.

if watab-catg1 LT watab1-catg1.

move : watab1-catg1 to watab2-catg1.

append itab1 from watab2.

else.

move : watab-catg1 to watab2-catg1.

append itab1 from watab2.

endif.

if watab-catg2 LT watab1-catg2.

move : watab1-catg2 to watab2-catg2.

modify itab1 from watab2.

else.

move : watab-catg2 to watab2-catg2.

modify itab1 from watab2.

endif.

endif.

endloop.

reward if helpful.

Regards,

nagaraj

Former Member
0 Kudos

Hi,

Go through example

DATA: BEGIN OF connection,

cityfrom TYPE spfli-cityfrom,

cityto TYPE spfli-cityto,

distid TYPE spfli-distid,

distance TYPE spfli-distance,

END OF connection.

DATA connection_tab LIKE SORTED TABLE OF connection

WITH NON-UNIQUE KEY cityfrom cityto

distid distance.

SELECT cityfrom cityto distid distance

FROM spfli

INTO TABLE connection_tab.

DELETE ADJACENT DUPLICATES FROM connection_tab.

Reward if useful.

Former Member

check it out with delete adjacent duplicate records

Deleting Adjacent Duplicate Entries

To delete adjacent duplicate entries use the following statement:

DELETE ADJACENT DUPLICATE ENTRIES FROM <itab>

[COMPARING <f1> <f 2> ...

|ALL FIELDS].

The system deletes all adjacent duplicate entries from the internal table <itab>. Entries are duplicate if they fulfill one of the following compare criteria:

Without the COMPARING addition, the contents of the key fields of the table must be identical in both lines.

If you use the addition COMPARING <f1> <f 2> ... the contents of the specified fields <f 1 > <f 2 > ... must be identical in both lines. You can also specify a field <f i > dynamically as the contents of a field <n i > in the form (<n i >). If <n i > is empty when the statement is executed, it is ignored. You can restrict the search to partial fields by specifying offset and length.

If you use the addition COMPARING ALL FIELDS the contents of all fields of both lines must be identical.

You can use this statement to delete all duplicate entries from an internal table if the table is sorted by the specified compare criterion.

If at least one line is deleted, the system sets SY-SUBRC to 0, otherwise to 4.

hymavathi_oruganti
Active Contributor
0 Kudos

no u cant use delete adjacent duplicates and get the o/p u needed in this case.

one solution is collect statement, while inserting record itself, if u use collect u can get the o/p.

or else u have to write some logic

Former Member
0 Kudos

Thanks all of u , By using the collect statement i am able to get the desired result

former_member197578
Participant
0 Kudos

Hi ABAPers,

You may use following logic to delete duplicate entries from an internal table and show in the output the specific entry which is duplicate as an error:

Let say ITAB1[ ] has all your data. Sort it based on key and then assign it to another temporary table ITAB2[ ].

i.e. ITAB1[ ] = ITAB2[ ].

Now, DELETE ADJACENT DUPLICATES from table ITAB2[ ] comparing the key. Now ITAB2[ ] has all but unique entries from ITAB1 [ ] while your original table ITAB1[ ] has all the entries (Including duplicate ones).

Now, LOOP AT ITAB1[ ] INTO DATA(LWA_ITAB1). (Loop at original table)

*** "Start of Comment" Inside this LOOP, READ ITAB2[ ] and delete it for that particular entry If SY-SUBRC = 0 (If read is successful). This deletion would remove that particular unique entry from ITAB2[ ] and now if it's READ again for the same entry it should throw an error (Sy-subrc <> 0) and at the same time it would also mean that it's being read for a duplicate entry from ITAB1[ ].

So now, if next time in ITAB1[ ]LOOP sy-subrc <> 0, this would mean that the current entry is duplicate as it was already found once and hence deleted. *** End of Comment

** PSEUDOCODE

READ TABLE ITAB2[ ] INTO DATA(lwa_itab2) WITH KEY KEY1 = .LWA_ITAB1-KEY1.

IF SY-SUBRC = 0.

DELETE ITAB2[ ] WHERE KEY1 = LWA_ITAB1-KEY1.

ELSE.

Throw an error or append error to return table saying KEY1 has duplicate entries.

ENDIF.

ENDLOOP.