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

Delete duplicate records in internal table

Former Member
0 Likes
2,143

Hi Friends,

I am working on a report in which i have to delete duplicate records from an internal table. But the order of entries should not change. So when i use sort and delete adjacent duplicates the order is changed.

Can someone throw some light on this.

Thanx,

Britto J

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,061

Hi Britto Manohar ,

\

take another internal table,itab2 with extra field 'order'.

then move coressponding filds to that second atble.loop at itab2 assaign order from 1 to ....

sort itab2.

delete adjacent duplicate..

sort itab2 by order.

move coressponding fields of itab2 to itab1.

Reagards,

Kiran B

Hi Friends,

I am working on a report in which i have to delete duplicate records from an internal table. But the order of entries should not change. So when i use sort and delete adjacent duplicates the order is changed.

Can someone throw some light on this.

Thanx,

Britto J

6 REPLIES 6
Read only

Former Member
0 Likes
1,061

Hello,

U can use this syntax

DELETE ADJACENT DUPLICATES COMPARING FIELDS1 ......

Hope this will useful for u.....

Read only

Former Member
0 Likes
1,061

Then you will have to manually loop at the internal table and find the duplicates. Copy the internal table into another one, loop at the first table and check the second table for similar entries, if there are more than one, you can retain one and delete the rest.

Regards,

Ravi

Note: Please mark the helpful answers

Read only

Former Member
0 Likes
1,061

Hi,

since u dont want sort u can make to table of same type.


itab1
itab2.

append lines of itab1 to itab2.

sort itab2 by key.

delete adjacent duplicates from itab2.

loop at itab1.
   read table itab2
   with key abc = itab2-abc
   transporting no fields
   binary search.
   if sy-subrc <> 0.
      delete itab1.
   endif.
endloop.

In this way u will be able to delete duplicate entries

without sorting.

Binarch search & transporting no field will speed up the

process

cross check the syntax.

After the execution u can discard the itab2.

feel free to ask further question

<b>Mark Helpful Answers</b>

Read only

Former Member
0 Likes
1,062

Hi Britto Manohar ,

\

take another internal table,itab2 with extra field 'order'.

then move coressponding filds to that second atble.loop at itab2 assaign order from 1 to ....

sort itab2.

delete adjacent duplicate..

sort itab2 by order.

move coressponding fields of itab2 to itab1.

Reagards,

Kiran B

Read only

0 Likes
1,061

Hi,

when you use Delete adjacenet duplicates it should be sorted. since in your case you don't want to sort then how can you delete.find rite now in which order the data is present. then based on that you have to loop the itab and delete the records. but which record you will delete also matters.

accordingly code.

Regards

vijay

Read only

Former Member
0 Likes
1,061

Hi,

Refer this & i think u should go for my post given above.

<b>This is from SAP Help.</b>

DELETE ADJACENT DUPLICATES FROM itab.

Extras:

1. ... COMPARING f1 f2 ...

2. ... COMPARING ALL FIELDS

Effect

Deletes adjacent duplicate entries from the internal table itab. If there are n duplicate entries in succession, the first entry is retained, and the following n-1 entries are deleted.

Two lines are regarded as duplicates if their keys are identical.

The Return Code is set as follows:

SY-SUBRC = 0:

At least one duplicate was found, and at least one entry was deleted.

SY-SUBRC = 4:

No duplicates found, no entries deleted.

Addition 1

... COMPARING f1 f2 ...

Effect

Two lines of the internal table itab are regarded as duplicates if they have identical contents in the fields f1, f2, ...

Addition 2

... COMPARING ALL FIELDS

Effect

Two lines of the internal table are regarded as duplicates if all of their field contents are identical.

Notes

The DELETE ADJACENT DUPLICATES statement works particularly well if you have sorted the internal table itab according to the fields that you want to compare when looking for duplicates. In this case, deleting adjacent duplicates is the same as deleting all duplicates. The direction of the sort is irrelevant.

If you do not know a comparison expression until runtime, you can specify it dynamically as the contents of the field name in the expression COMPARING ... (name) .... If name is empty at runtime, the comparison expression is ignored. If name contains an invalid component name, a runtime error occurs.

You can further restrict comparison expressions - both static and dynamic - by specifying offset and length.

Note