‎2009 Jul 08 11:51 AM
Hi experts,
How can I find duplicatind data in an internal table?
Thanx for the assistance
‎2009 Jul 09 6:16 AM
hi check this will work
{code]
TYPES:BEGIN OF ty_it,
field1 TYPE c,
field2 TYPE c,
END OF ty_it.
DATA:it TYPE TABLE OF ty_it.
DATA:it1 TYPE TABLE OF ty_it.
DATA:wa TYPE ty_it.
DATA:wa1 TYPE ty_it.
data:count type i.
wa-field1 = 'A'.wa-field2 = 'b'.APPEND wa TO it.
wa-field1 = 'A'.wa-field2 = 'b'.APPEND wa TO it.
wa-field1 = 'C'.wa-field2 = 'b'.APPEND wa TO it.
wa-field1 = 'D'.wa-field2 = 'b'.APPEND wa TO it.
wa-field1 = 'C'.wa-field2 = 'b'.APPEND wa TO it.
wa-field1 = 'B'.wa-field2 = 'b'.APPEND wa TO it.
sort it.
it1[] = it[].
delete adjacent duplicates from it1.
loop at it1 into wa1.
loop at it into wa where field1 = wa1-field1 and field2 = wa1-field2. .
count = count + 1.
if count = 2.
exit.
endif.
endloop.
if count = 1.
delete it where field1 = wa1-field1 and field2 = wa-field2.
endif.
clear count.
endloop.
LOOP AT it INTO wa.
WRITE:/ wa-field1,wa-field2.
ENDLOOP.
{code}
‎2009 Jul 08 11:56 AM
Hi,
Sort the internal table with the field which u want to see if they are duplicated.
for example
sort itab by matnr.
now in the internal table u will have the material number reocrds in ascending order . IF one material has come thrice , then first three records will be same material.
now u can use the statement to delete the duplcate entries.
delete duplciate entries from table itab comparing matnr.
Regards,
Nagaraj
‎2009 Jul 08 1:17 PM
But the problem is that I dont want to delete the duplicating records instead I want to use the records that are not duplicated and delete that ones that are not duplicated.
‎2009 Jul 08 1:26 PM
I want to use the records that are not duplicated
delete that ones that are not duplicated......
want to use or delete ??
i think you want the records that are not duplicated.
Example:
begin of itab occurs 0,
fld type c,
fld1 type c,
end of itab.
itab-fld = 'A'.
itab-fld1 = '1'.
append itab.
itab-fld = 'A'.
itab-fld1 = '2.
append itab.
itab-fld = B'.
itab-fld1 = '2.
append itab.
loop at itab.
at new fld.
continue.
endat.
delete itab index sy-tabix. "deletes the duplicate
endloop.
Make some workarounds and do this
‎2009 Jul 08 1:51 PM
I want the records that are not duplicated to be deleted and the ones that are duplicating to stay as they are
‎2009 Jul 08 2:00 PM
sort itab by field.
loop at itab.
at new field.
delete itab index sy-tabix.
endat.
endloop.
‎2009 Jul 08 2:15 PM
sorry my previous post will not work.
types:BEGIN OF ty_it,
field1 TYPE c,
field2 TYPE c,
END OF ty_it.
data:it type TABLE OF ty_it.
data:wa type ty_it.
data:wa1 type ty_it.
wa-field1 = 'A'.wa-field2 = 'b'.APPEND wa to it.
wa-field1 = 'A'.wa-field2 = 'b'.APPEND wa to it.
wa-field1 = 'C'.wa-field2 = 'b'.APPEND wa to it.
wa-field1 = 'D'.wa-field2 = 'b'.APPEND wa to it.
wa-field1 = 'C'.wa-field2 = 'b'.APPEND wa to it.
wa-field1 = 'B'.wa-field2 = 'b'.APPEND wa to it.
sort it by field1.
loop at it into wa.
at new field1.
read table it index sy-tabix into wa1.
continue.
endat.
if wa-field1 = wa1-field1.
delete it where field1 = wa1-field1 .
else.
read table it index sy-tabix into wa1.
endif.
endloop.
loop at it into wa.
write:/ wa-field1,wa-field2.
endloop.
Pl try to avoid the delete statements by moving it to other itab.
There may many other better methods than this
‎2009 Jul 08 2:41 PM
‎2009 Jul 08 4:42 PM
copy itab1 to itab2.
loop at itab1.
loop at itab2.
delete row if key fields of itab2 are same as itab1. "delete the current row.
endloop of itab2.
read from itab2 for keys same current itab1 values. "the current row has been deleted. if you get a hit again, its dup.
if sy-subrc = 0, you have a duplicate row.
if sy-subrc <> 0, a uniq row.
endloop itab1.
Edited by: brd on Jul 8, 2009 11:43 AM
‎2009 Jul 08 11:56 AM
Hi
If you do not want to have the duplicate entries based on certain field combination, you can delete these entries using
DELETE ADJACENT DUPLICATES FROM itab COMPARING fld 1 fld2..
Cheers
Sourav
‎2009 Jul 08 11:58 AM
Hi,
This is a very basic question.. just sort the table with the required fields to know if it contains duplicate records or not....
‎2009 Jul 08 12:01 PM
Hi,
There is no KEYWORD available to find duplicate data between 2 internal tables.
What you can do is as follows:-
Suppose your main data is in ITAB1.
Create a same internal table ITAB2 and do as follows
Sort ITAB1.
ITAB2[] = ITAB1[]
Delete adjacent duplicates from ITAB2 comparing all fields.
IF ITAB2 EQ ITAB1
* duplicate reocrds does not exist
ELSE
* duplicate records exists
ENDIF.Regads,
Ankur Parab
‎2009 Jul 08 1:34 PM
Hi!
Sort ITAB1.
ITAB2[] = ITAB1[]
Delete adjacent duplicates from ITAB2 comparing all fields.
LOOP AT ITAB2 into WA2.
DELETE TABLE ITAB1 FROM WA2.
END LOOP.
where work area WA2 have the same structure of ITAB1 and ITAB2.
Regards.
Sarbajit.
‎2009 Jul 08 2:18 PM
Hi,
You first have to sort your internal table,like
SORT ITAB. then you have to write, DELETE ADJACENT DUPLICATES FROM ITAB. this will remove the duplicate entries from your internal table.
Hope it helps
Regards
Mansi
‎2009 Jul 09 6:16 AM
hi check this will work
{code]
TYPES:BEGIN OF ty_it,
field1 TYPE c,
field2 TYPE c,
END OF ty_it.
DATA:it TYPE TABLE OF ty_it.
DATA:it1 TYPE TABLE OF ty_it.
DATA:wa TYPE ty_it.
DATA:wa1 TYPE ty_it.
data:count type i.
wa-field1 = 'A'.wa-field2 = 'b'.APPEND wa TO it.
wa-field1 = 'A'.wa-field2 = 'b'.APPEND wa TO it.
wa-field1 = 'C'.wa-field2 = 'b'.APPEND wa TO it.
wa-field1 = 'D'.wa-field2 = 'b'.APPEND wa TO it.
wa-field1 = 'C'.wa-field2 = 'b'.APPEND wa TO it.
wa-field1 = 'B'.wa-field2 = 'b'.APPEND wa TO it.
sort it.
it1[] = it[].
delete adjacent duplicates from it1.
loop at it1 into wa1.
loop at it into wa where field1 = wa1-field1 and field2 = wa1-field2. .
count = count + 1.
if count = 2.
exit.
endif.
endloop.
if count = 1.
delete it where field1 = wa1-field1 and field2 = wa-field2.
endif.
clear count.
endloop.
LOOP AT it INTO wa.
WRITE:/ wa-field1,wa-field2.
ENDLOOP.
{code}