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 duplicate in same internal table

Former Member
0 Likes
12,370

Hi,

I have an internal table which has three fields(F1,F2,F3).

Now suppose I have 5 records and I have to compare whether the fields(F1 and F3) of record 1st and 3rd are same or not(F2 is common for both records).Can you suggest any syntax?

1 ACCEPTED SOLUTION
Read only

faisalatsap
Active Contributor
3,130

Hi,

Please test the following Sample Code hope will solve out your problem

TYPES:  BEGIN OF ty_test,
        f1(3),
        f2(3),
        f3(3),
        END OF ty_test.
DATA: it_test TYPE STANDARD TABLE OF ty_test WITH HEADER LINE,
      old_f1(3),
      old_f3(3).

it_test-f1 = 'AAA'. it_test-f2 = 'AAA'. it_test-f3 = 'DDD'. APPEND it_test.
it_test-f1 = 'BBB'. it_test-f2 = 'AAA'. it_test-f3 = 'CCC'. APPEND it_test.
it_test-f1 = 'AAA'. it_test-f2 = 'AAA'. it_test-f3 = 'AAA'. APPEND it_test.
it_test-f1 = 'BBB'. it_test-f2 = 'AAA'. it_test-f3 = 'CCC'. APPEND it_test.

SORT: it_test by f1 f3.

LOOP AT it_test.
  IF old_f1 = it_test-f1 and old_f3 = it_test-f3.
    WRITE: 'There is Duplicate Record on Index ', sy-tabix.
  ENDIF.
  old_f1 = it_test-f1.
  old_f3 = it_test-f3.
ENDLOOP.

Please Reply if any problem

Best Regards,

Faisal

6 REPLIES 6
Read only

Former Member
0 Likes
3,130

Hi

I don't think there is a dirrent command.We may need to put logic around the internal table to compare the records.

Regards

Gaurav Arora

Read only

Former Member
3,130

hi,

You can try this:

1. sort itab by fields F1 and F3.

2. Delete adjacent duplicates comparing F1 and F3.

Regards,

Komal

Read only

faisalatsap
Active Contributor
3,131

Hi,

Please test the following Sample Code hope will solve out your problem

TYPES:  BEGIN OF ty_test,
        f1(3),
        f2(3),
        f3(3),
        END OF ty_test.
DATA: it_test TYPE STANDARD TABLE OF ty_test WITH HEADER LINE,
      old_f1(3),
      old_f3(3).

it_test-f1 = 'AAA'. it_test-f2 = 'AAA'. it_test-f3 = 'DDD'. APPEND it_test.
it_test-f1 = 'BBB'. it_test-f2 = 'AAA'. it_test-f3 = 'CCC'. APPEND it_test.
it_test-f1 = 'AAA'. it_test-f2 = 'AAA'. it_test-f3 = 'AAA'. APPEND it_test.
it_test-f1 = 'BBB'. it_test-f2 = 'AAA'. it_test-f3 = 'CCC'. APPEND it_test.

SORT: it_test by f1 f3.

LOOP AT it_test.
  IF old_f1 = it_test-f1 and old_f3 = it_test-f3.
    WRITE: 'There is Duplicate Record on Index ', sy-tabix.
  ENDIF.
  old_f1 = it_test-f1.
  old_f3 = it_test-f3.
ENDLOOP.

Please Reply if any problem

Best Regards,

Faisal

Read only

Former Member
0 Likes
3,130

hi Ginger ..

You need to sort at the internal table and than compare the record and delete if same

sort it_tab  by F1 F2.
delete adjacent duplicates from it_tab  comparing F1 F2.

thanks

Read only

Former Member
0 Likes
3,130

HI,

Exactly, I did not get what you trying to say, but what I have understand is that, in your internal table you have to check if the record 1 , 3, or on any number is same or not , ie. value of F1 & F3 field, is same or not, and value of F2 is common in both the records,

So, you can sort the table on F1, F3 & F2 and if you want to remove the duplicate entries you can use delete adjuscent duplicates,

Or you can populate same data in one internal table say ITAB1 and then , you can loop at ITAB AND READ TABLE iITAB1 WITH KEY F1-ITAB-F1 AND F3-ITAB-F3 AND IF SY-SUBRC EQ 0, YOU CAN FIND THE records where you are getting duplicates, then according to your next program logic you can proceed.

Hope this may helpful to you

Rani

Read only

Former Member
0 Likes
3,130

Hi

sort the inetrnal table

use read stmt

READ TABLE itab INTO wa COMPARING f1 f2 ...

Thanks

SUbha