‎2007 May 16 5:01 AM
Hi Experts,
I would like to check that, are there some duplicate rows in internal table?
I can chek it by hard code and I think it will not work when working with a lot of row.
Could you please tell me the solutions for this case?
Is there FM for this?
Thanks in advance.
Regards,
Tiwa.
Here is my code
REPORT.
DATA: BEGIN OF t_test OCCURS 0,
f1, f2, f3,
END OF t_test.
DATA: t_tmp1 LIKE t_test OCCURS 0 WITH HEADER LINE,
t_tmp2 LIKE t_test OCCURS 0 WITH HEADER LINE.
DATA found TYPE I.
PERFORM add USING 'a' 'b' 'c'.
PERFORM add USING 'a' 'c' 'c'.
PERFORM add USING 'a' 'b' 'c'.
PERFORM add USING 'a' 'd' 'd'.
PERFORM add USING 'a' 'c' 'c'.
t_tmp1[] = t_test[].
t_tmp2[] = t_test[].
LOOP AT t_tmp1.
CLEAR found.
LOOP AT t_tmp2.
IF ( t_tmp1-f1 EQ t_tmp2-f1 ) AND
( t_tmp1-f2 EQ t_tmp2-f2 ) AND
( t_tmp1-f3 EQ t_tmp2-f3 ).
ADD 1 TO found.
ENDIF.
ENDLOOP.
IF found > 1.
WRITE: / t_tmp1-f1, t_tmp1-f2, t_tmp1-f3, 'Duplicate'.
DELETE t_tmp1 WHERE f1 = t_tmp1-f1
AND f2 = t_tmp1-f2
AND f3 = t_tmp1-f3.
DELETE t_tmp2 WHERE f1 = t_tmp1-f1
AND f2 = t_tmp1-f2
AND f3 = t_tmp1-f3.
ENDIF.
ENDLOOP.
FORM add USING f1 f2 f3.
t_test-f1 = f1.
t_test-f2 = f2.
t_test-f3 = f3.
APPEND t_test.
ENDFORM.
‎2007 May 16 5:20 AM
hi
one more method
loop at itab1.
read table itab2 with key f1 eq itab1-f1 f2 eq itab1-f2 f3 eq itab1-f3.
if sy-subrc eq 0.
write : 'duplicate exists for ', itab1-f1, itab1-f2, itab1-f3.
else.
move-corresponding itab1 to itab2.
append itab12.
endloop.
then use delete adjacent duplicates to remove duplicate entries
if helpful, reward
Sathish. R
‎2007 May 16 5:05 AM
you can use :
Delete adjacent duplicated from Itab comparing <field>
eg :
Delete adjacent duplicated from Itab comparing BUKRS.
This will give you only one entry in the internal table for a perticular BUKRS
sort the itab first.
please reward if useful.
reagrds,
Message was edited by:
Sachin123
‎2007 May 16 5:12 AM
Hi Sachin123,
Thanks for your reply.
I known that I can use delete adjacent for delete duplicates row by comparing key... but in this case I want to know which rows, data duplicates.
Regards,
Tiwa.
‎2007 May 16 5:19 AM
HI,
see my previous post..It clearly says that duplicate entry exists for the particular row..
I am posting the code again...
wa and wa are work area of type internal table type
..
loop at itab into wa.
read table itab into wa1 with key f1 = wa-f1.
if sy-subrc = 0.
if ( wa-f2 = wa1-f2 AND wa-f3 = wa1-f3). (LKike this check to no of fields..
<b>write: 'Duplicate exists for row:", wa-f1, wa-f2, wa-f3.</b> "else you can give index here
endif.
endif.
endloop.
rewards if useful
regards,
nazeer
‎2007 May 16 5:24 AM
Loop on Itab1.
Read ITAB2 with all keys of ITAB1.
if sy-subrc = 0.
duplicate.else not.
regards,
‎2007 May 16 5:06 AM
Use syntax:
DELETE ADJACENT DUPLICATES FROM itab COMPARING field1 field2...Reward points if useful.
Regards.
‎2007 May 16 5:09 AM
hi,
for checking the duplicates exist..
Loop your internal table..
eg:
loop at itab into wa.
read table itab into wa1 with key f1 = wa-f1.
if sy-subrc = 0.
if ( wa-f2 = wa1-f2 AND wa-f3 = wa1-f3). (LKike this check to no of fields..
write: 'Duplicate exists for row:", wa-f1, wa-f2, wa-f3.
endif.
endif.
endloop.
IF you want to delete duplicate entries then see above post..
DELETE ADJACENT DUPLICATES FROM ITAB COMPARING ALL FIELDS.
Sort your table before using this, because once I had a problem in deleteing adjacent duplicates.. when I sorted the table It was working fine..
rewards if useful
regards,
nazeer
Message was edited by:
nazeer shaik
‎2007 May 16 5:15 AM
hi
good
in this case you can use like this
1-first use sort internal table to sort all the fields of internal table
2-second use delete itab to delete the null field of the internal table
thanks
mrutyun^
‎2007 May 16 5:20 AM
hi
one more method
loop at itab1.
read table itab2 with key f1 eq itab1-f1 f2 eq itab1-f2 f3 eq itab1-f3.
if sy-subrc eq 0.
write : 'duplicate exists for ', itab1-f1, itab1-f2, itab1-f3.
else.
move-corresponding itab1 to itab2.
append itab12.
endloop.
then use delete adjacent duplicates to remove duplicate entries
if helpful, reward
Sathish. R
‎2007 May 16 5:32 AM
Hi All,
If itab2 be assigned data from itab1 then all of them are same,
loop at itab1.
read table itab2 with key f1 eq itab1-f1 f2 eq itab1-f2 f3 eq itab1-f3.
sy-subrc = 0 always
Do you think so?
Regards,
Tiwa.
‎2007 May 16 5:40 AM
hi
nope...first time, itab2 will not contain any data of itab1..so subrc is 4, in that loop moves record of itab1...next loop, it reads the itab2 with keys of itab1, if it finds one then, it means a record of same entries in current loop of itab1 exists, which means that the current line of itab1 is a duplicate record...reply back for queries...
execute this code and see the output
DATA: BEGIN OF t_test OCCURS 0,
f1, f2, f3,
END OF t_test.
DATA: itab1 LIKE t_test OCCURS 0 WITH HEADER LINE.
PERFORM add USING 'a' 'b' 'c'.
PERFORM add USING 'a' 'c' 'c'.
PERFORM add USING 'a' 'b' 'c'.
PERFORM add USING 'a' 'd' 'd'.
PERFORM add USING 'a' 'c' 'c'.
LOOP AT t_test.
read table itab1 with key f1 = t_test-f1 f2 = t_test-f2 f3 = t_test-f3.
if sy-subrc eq 0.
write : / 'duplicate exists for ', t_test-f1, t_test-f2, t_test-f3.
else.
move-corresponding t_test to itab1.
append itab1.
endif.
endloop.
FORM add USING f1 f2 f3.
t_test-f1 = f1.
t_test-f2 = f2.
t_test-f3 = f3.
APPEND t_test.
ENDFORM.
if helpful, reward
Sathish. R
‎2007 May 16 5:48 AM
Hi Sathish R,
I use your code to apply and the problem is solved
I have already give you 10 points.
Thanks everyone,
Tiwa.
-
DATA: w_tmp1 LIKE t_tmp1.
LOOP AT t_tmp1.
IF t_tmp1 EQ w_tmp1.
WRITE: / t_tmp1-f1, t_tmp1-f2, t_tmp1-f3, 'Duplicates'.
DELETE t_tmp1 WHERE f1 = t_tmp1-f1
AND f2 = t_tmp1-f2
AND f3 = t_tmp1-f3.
ENDIF.
w_tmp1 = t_tmp1.
ENDLOOP.
‎2007 May 16 6:22 AM
Its great you have given him 10 points but what about the rest of us who took the time to read your issue and respond trying to help you out?? Do you think we have nothing better to do at work??
All replies should at least be rewarded with 2 points....please read the <b>Rules of Engagement</b> at the top of the forum threads.
Thanks.
‎2007 May 16 5:36 AM
Hi,
This code helps you out.
define a variable wrk_tabix like sy-tabix and apply this logic.
loop at t_tmp1.
wrk_tabix = sy-tabix.
loop at t_tmp2 where f1 = t_tmp1-f1 and
f2 = t_tmp1-f2 and
f3 = t_tmp1-f3.
write:/ 'duplicate', t_tmp2-f1,t_tmp2-f2,t_tmp2-f3.
delete t_tmp1 index wrk_tabix.
delete t_tmp2 index wrk_tabix.
endloop.
endloop.
Regards,