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: 

Duplicate Values in an Internal Table

Former Member
0 Kudos

Hi,

Is it possible to delete records having Duplicate Values in a field, in an Internal Table, but in the situation when the Internal Table is NOT sorted on that field ?

Regards,

Pankaj.

1 ACCEPTED SOLUTION

Clemenss
Active Contributor
0 Kudos

Hi,

quick&dirty, but efficient and fast:

(if itab is an internal table with header line)

<pre>

DATA: itab_sort LIKE HASHED TABLE OF itab WITH UNIQUE KEY table_line.

FIELD-SYMBOLS: <any> TYPE ANY.

LOOP AT itab ASSIGNING <any>.

INSERT <any> INTO TABLE itab_sort.

IF sy-subrc <> 0.

DELETE itab.

ENDIF.

ENDLOOP.

</pre>

Duplicate entries deleted regardless of sort order.

Regards,

Clemens

13 REPLIES 13

former_member181962
Active Contributor
0 Kudos

No.

anversha_s
Active Contributor
0 Kudos

hi,

u shuld sort with respect to that value.

thn only u can delete .

<i>chk this sample.</i>


report zsha_0001.
 
 
data: begin of itab1 occurs 0,
      fld1 type c,
      fld2 type c,
      fld3 type c,
      end of itab1.
 
data: begin of itab2 occurs 0,
      fld1 type c,
      fld2 type c,
      fld3 type c,
      end of itab2.
 
data: counter type i.
 
itab1 = 'ABC'.  append itab1.
itab1 = 'DEF'.  append itab1.
itab1 = 'GHI'.  append itab1.
itab1 = 'DEF'.  append itab1.
itab1 = 'GHI'.  append itab1.
itab1 = 'DEF'.  append itab1.
 
 
itab2[] = itab1[].
 
sort itab1 ascending.
delete adjacent duplicates from itab1.
 
loop at itab1.
 
clear counter.
  loop at itab2 where fld1 = itab1-fld1
                 and  fld2 = itab1-fld2
                 and  fld3 = itab1-fld3.
    counter = counter + 1.
  endloop.
 
 write:/ itab1-fld1, itab1-fld2, itab1-fld3,
         'Number of occurances:', counter.
 
endloop.

rgds

anver

Former Member
0 Kudos

It is, but you will have to code it yourself. The command 'DELETE ADAJCENT DUPLICATES...' requires the fields to be sorted.

Former Member
0 Kudos

try collect statement.

Former Member
0 Kudos

Hello

If u don't want to sort the table u need to loop the table and find the duplicate entries.

If useful reward..

Vasanth

Former Member
0 Kudos

it is not possible to delete using the syntax of adjacent duplicates.

but you can use a loop within a loop but this will cause a lot of performance problems

e.g

loop at itab into wa_itab.

loop at itab into wa_itab1 where field = wa_itab-field.

if cnt gt 1.

delete itab from wa_itab1.

endif.

endloop.

endloop.

Former Member
0 Kudos

Hi,

you can do this, but it is very bad if we look at performence side.

move the data to another internal table.

Loop the Table, and loop the other internal table with that filed using Index

if you find that set a Flag = 1, if you find that same again then delete that record.

but this is not a best way, it is better you sort the talbe

Regards

Sudheer

Former Member
0 Kudos

hi,

this is possible,

delete adjacent duplicates in internal table name by key values

this is the code............before deleting the internal table u have to sort pankaj

Former Member
0 Kudos

Hi Pankaj,

i think you have to create a copy of your itab and collect.

try this:

REPORT ZGRO_TEST.

*

DATA: BEGIN OF ITAB OCCURS 0,

MATNR LIKE MARA-MATNR,

VBELN LIKE VBAK-VBELN,

END OF ITAB.

*

DATA: BEGIN OF ITAB1 OCCURS 0,

MATNR LIKE MARA-MATNR,

VBELN LIKE VBAK-VBELN,

END OF ITAB1.

START-OF-SELECTION.

*

itab-matnr = '000000000000000111'. itab-vbeln = '0000000011'. append itab.

itab-matnr = '000000000000000113'. itab-vbeln = '0000000013'. append itab.

itab-matnr = '000000000000000111'. itab-vbeln = '0000000011'. append itab.

itab-matnr = '000000000000000112'. itab-vbeln = '0000000012'. append itab.

itab-matnr = '000000000000000111'. itab-vbeln = '0000000011'. append itab.

itab-matnr = '000000000000000112'. itab-vbeln = '0000000012'. append itab.

itab-matnr = '000000000000000113'. itab-vbeln = '0000000013'. append itab.

*

loop at itab.

itab1 = itab.

collect itab1.

endloop.

loop at itab.

write: / itab-matnr, itab-vbeln.

endloop.

write: / sy-uline.

loop at itab1.

write: / itab1-matnr, itab1-vbeln.

endloop.

END-OF-SELECTION.

*

if you whant it back in itab. refrash itab and copy itab1 to itab.

Hope it helps.

Regards, Dieter

0 Kudos

Will 'Collect' not add up the numeric values.

As in this case,

000000000000000111 will finally be 000000000000000333

000000000000000112 will finally be 000000000000000224

000000000000000113 will finally be 000000000000000226

And I want the original values!

0 Kudos

Hi Pankaj,

if you have numeric values you are right, but in my example

are no numeric values. Try my example and you will see it.

regards, Dieter

Clemenss
Active Contributor
0 Kudos

Hi,

quick&dirty, but efficient and fast:

(if itab is an internal table with header line)

<pre>

DATA: itab_sort LIKE HASHED TABLE OF itab WITH UNIQUE KEY table_line.

FIELD-SYMBOLS: <any> TYPE ANY.

LOOP AT itab ASSIGNING <any>.

INSERT <any> INTO TABLE itab_sort.

IF sy-subrc <> 0.

DELETE itab.

ENDIF.

ENDLOOP.

</pre>

Duplicate entries deleted regardless of sort order.

Regards,

Clemens

Former Member
0 Kudos

Thanks Clemens.