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

Check duplicates

Former Member
0 Likes
3,861

Hi,

How would I check duplicate records in an internal table?

Thanks

Will

1 ACCEPTED SOLUTION
Read only

gopi_narendra
Active Contributor
0 Likes
1,806

delete adjacent duplicates

press F1 u can see the help for it. and also the syntax n usage

Regards

- Gopi

12 REPLIES 12
Read only

gopi_narendra
Active Contributor
0 Likes
1,807

delete adjacent duplicates

press F1 u can see the help for it. and also the syntax n usage

Regards

- Gopi

Read only

0 Likes
1,806

Hi,

Actually I wouldn't want to delete the duplicate; I would want to put in another internal table.

Thanks

Read only

0 Likes
1,806

Hi,

I assume that f1 and f2 are the fields in itab which needs to be cehcked for duplication.

sort itab by f1 f2.

itab_temp[] = itab[].

*itab_temp will now contains all the entries of itab.

loop at itab_temp into wa.

loop at itab_temp into wa1 where f1 = wa-f1 and f2 = wa-f2.

append wa1 to itab_dup.

endloop.

delete itab_temp where f1 = wa-f1 and f2 = wa-f2.

endloop.

Now itab_dup will contain duplicate entries and itab_temp will contain only distinct entries.itab contains all entries.

Read only

0 Likes
1,806

Hi ,

If i understood your requirement correctly, it goes like this. You have two internal tables itab1 and itab2 .itab1 will be having your required data with duplicate information also . In itab2 you want to put all unique information of itab1 .

then it is quite easy , just try this.

Assuming two internals table have same structure

1. If your internal table is with header line.

itab2[] = itab1[] .

(or)

.If your internal table is without header line

itab2 = itab1 .

2. apply delete adjacent on itab2 not itab1 .

sort itab2 by field1 ..

delete adjacent duplicateds from itab2 .

Like this you have all information in itab1 and itab2 will be containing unique information .

Regards,

Raghav

Read only

Former Member
0 Likes
1,806

Hi ,

One way is to loop on the table with the key of your current record and check number of times the loop is executed.

Do you want to just check duplicate records or want to delete the duplicate records , if you want to delete then used the command <b>DELETE ADJACENT DUPLICATES FROM itab</b>, but please make sure that you sort the table before you use this command , becuase if it is not sorted this command will not give the desired result.

Regards

Arun

Read only

Former Member
0 Likes
1,806

Hi will,

First sort your internal table based on your primary key.

Primary key means "on what base you want to find duplicate records. "

sort itab by <your primary key>

1. If you want to delete your duplicate records.Use

Delete adjacent duplicates from table itab by comparing ' your primary key.'

Pls. reward points for useful answers

Read only

Former Member
0 Likes
1,806

Hi,

If you want to put it in another internal table .

sort itab by <primary key>.

Loop at itab.

At new <primary key>.

  • If itab1 and itab2 have different structures

move-corresponding itab1 to itab2

    • If itab1 and itab2 have same structures structures

itab2[] = itab1[]

endat.

endloop.

Pls. reward points if useful

Read only

Former Member
0 Likes
1,806

hi can you tell what is your exact requirement?

other wise you can do

loop at itab.

counter = counter + 1.

move-corresponding itab to itab1.

append itab1.

at end of <f1>.

if conter gt 2.

move-corresponding itab to itab1.

append itab1.

append lines of itab1 to itab2.

clear counter.

refresh itab1.

else.

clear counter.

refresh itab1.

endif.

endat.

endloop.

regards

shiba dutta

endloop.

Read only

0 Likes
1,806

Hi this si the code which can give u the list of the Key field of the records which are having more than one records, prerequestisite is tht "u shud have atleast a primary key in ur internal table"..please change the code as per ur requrimetn ,it wil work and efficient too ,if number of records is too large in table -

Data : begin of itab occurs 0,

pernr like pernr-pernr,

endda like p0001-endda,

end of itab.

Data : begin of pernr_list occurs 0,

pernr like pernr-pernr,

end of pernr_list.

data : itab1 type range of itab WITH HEADER LINE,

itab2 like standard table of itab with header line.

DATA: DUPCOUNT TYPE I,

ORICOUNT TYPE I,

NEWCOUNT TYPE I,

TOTALHIT TYPE I VALUE 0.

Start-of-selection.

select pernr endda into corresponding fields of table itab

from pa0001

where endda = '99991231' and persg = p_eg and

pernr in ( select pernr from pa0000 where STAT2 = p_es

and endda = '99991231' ).

if sy-subrc = 0.

sort itab by pernr.

itab2[] = itab[].

DESCRIBE TABLE ITAB LINES ORICOUNT.

DELETE ADJACENT DUPLICATES FROM ITAB2.

DESCRIBE TABLE ITAB2 LINES NEWCOUNT.

DUPCOUNT = ORICOUNT - NEWCOUNT.

ITAB1-SIGN = 'I'.

ITAB1-OPTION = 'EQ'.

ITAB1-LOW = '00000000'.

APPEND ITAB1.

loop at itab.

IF NOT ITAB-PERNR IN ITAB1.

ITAB1-SIGN = 'I'.

ITAB1-OPTION = 'EQ'.

ITAB1-LOW = ITAB-PERNR.

APPEND ITAB1.

ELSE.

pernr_list-pernr = itab-pernr. "Contain key field Pernr of duplicate records

append pernr_list.

TOTALHIT = TOTALHIT + 1.

IF DUPCOUNT EQ TOTALHIT.

EXIT.

ENDIF.

ENDIF.

endloop.

Reward points if helpfull..

Read only

Sathish
Product and Topic Expert
Product and Topic Expert
0 Likes
1,806

DATA: itab1 type bkpf occurs 0,

itab2 type bkpf occurs 0,

wa_itab1 like bkpf,

f1_old type bkpf-bukrs,

f2_old type bkpf-gjahr.

loop at itab1 into wa_itab1.

if fl1_old not initial or f2_old not initial.

if fl_old EQ wa_itabl1-bukrs AND f2_old EQ wa_itab1-gjahr.

move-corresponding itab1 to itab2.

append itab2.

endif.

f1_old = wa_itab1-bukrs.

f2_old = wa_itab1-gjahr.

endloop.

I believe this should work. Small errors can be fixed easily.

Read only

Sathish
Product and Topic Expert
Product and Topic Expert
0 Likes
1,806

forgot to add in the above program. before endloop add the line delete adjacent duplicates.

Read only

RemiKaimal
Active Contributor
0 Likes
1,806

Hi Will,

You could try this :

SELECT f1

FROM ztable

INTO TABLE it_dup

GROUP BY f1

HAVING COUNT( * ) > 1.

Will get you the duplicate records based on field f1.

it_dup will hold the duplicate entries.

Hope this helps.

Regards,

Remi