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

Finding duplicating data

Former Member
0 Likes
1,531

Hi experts,

How can I find duplicatind data in an internal table?

Thanx for the assistance

1 ACCEPTED SOLUTION
Read only

kesavadas_thekkillath
Active Contributor
0 Likes
1,498

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}

14 REPLIES 14
Read only

former_member404244
Active Contributor
0 Likes
1,498

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

Read only

0 Likes
1,498

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.

Read only

0 Likes
1,498

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

Read only

0 Likes
1,498

I want the records that are not duplicated to be deleted and the ones that are duplicating to stay as they are

Read only

0 Likes
1,498
    
sort itab by field.
     loop at itab.
     at new field.
       delete itab index sy-tabix.
     endat.
     endloop.
Read only

0 Likes
1,498

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

Read only

0 Likes
1,498

that too will not work

Read only

0 Likes
1,498

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

Read only

Former Member
0 Likes
1,498

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

Read only

Former Member
0 Likes
1,498

Hi,

This is a very basic question.. just sort the table with the required fields to know if it contains duplicate records or not....

Read only

Former Member
0 Likes
1,498

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

Read only

sarbajitm
Contributor
0 Likes
1,498

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.

Read only

Former Member
0 Likes
1,498

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

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
1,499

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}