2006 Nov 14 1:47 PM
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.
2006 Nov 14 2:16 PM
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
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.
2006 Nov 14 1:49 PM
2006 Nov 14 1:49 PM
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
2006 Nov 14 1:50 PM
It is, but you will have to code it yourself. The command 'DELETE ADAJCENT DUPLICATES...' requires the fields to be sorted.
2006 Nov 14 1:51 PM
2006 Nov 14 1:51 PM
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
2006 Nov 14 1:53 PM
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.
2006 Nov 14 1:53 PM
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
2006 Nov 14 1:53 PM
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
2006 Nov 14 2:04 PM
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
2006 Nov 14 2:08 PM
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!
2006 Nov 14 2:24 PM
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
2006 Nov 14 2:16 PM
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
2006 Nov 14 2:26 PM