Application Development 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: 

How find and show duplicate data in a internal table.

Former Member
0 Kudos
5,941

Hi  everybody,

I´m new of this community and also new of SAP is my first mounth to work with abap.

I ask for your help for advice on how to solve the following problem:

I have an internal table with the follow field:

Mandt      Qnr             Material            Pos

010         000526        0019232           01

010         000526        0019232           02

010         000526        0823480           03

010         000526        0383348            01

010         000527        0019332            01

010         000527        0019332            02

....               ....                    ....                    ..

I need to find in this table all recod tha have the same  value of Mandt Qnr Material but with different Pos, and show in a table.

In the case of the previous example should be displayed:

010         000526        0019232           01

010         000526         0019232          02

010         000527        0019332            01

010         000527        0019332            02

Can show me how you can do?

Thank you in advance.

13 REPLIES 13

0 Kudos
1,097

Hi Tony,

If you want desired out put, you can sort your internal table and if you want to delete duplicate data you can apply code like

sort <internal_table> by matnr.

delete adjacent duplicates from <internal_table> comparing <key>.

Former Member
0 Kudos
1,097

data: lv_count type sy-tabix.

loop at itab into wa.

at new matnr.

     if sy-tabix ne 1.

       if lv_count gt 1.

          append wa to itab1.

       endif.

       clear lv_count.

     endif.

endat.

lv_count = lv_count + 1.

endloop.

Hope the above may help you.

Regards,

Vasu.

0 Kudos
1,097

Thank you  at all fro the for responses.

I´m sorry sreenivasulu bikumalla but I don´t understand this part:

at new matnr.

what´s means??

I want to see the program that I develop:

TYPES:BEGIN OF TY_IT,

       FIELD1 TYPE C,

       FIELD2 TYPE C,

       FIELD3 TYPE i,

      END OF TY_IT.

DATA:IT    TYPE STANDARD TABLE OF TY_IT.

DATA:IT1   TYPE STANDARD TABLE OF TY_IT.

DATA:WA    like line of IT.

DATA:WA1   like line of IT.

DATA:COUNT TYPE I.

WA-FIELD1 = 'A'.WA-FIELD2 = 'b'.WA-FIELD3 ='01'. APPEND WA TO IT.

WA-FIELD1 = 'A'.WA-FIELD2 = 'b'.WA-FIELD3 ='02'. APPEND WA TO IT.

WA-FIELD1 = 'A'.WA-FIELD2 = 'b'.WA-FIELD3 ='04'. APPEND WA TO IT.

WA-FIELD1 = 'A'.WA-FIELD2 = 'b'.WA-FIELD3 ='03'. APPEND WA TO IT.

WA-FIELD1 = 'C'.WA-FIELD2 = 'b'.WA-FIELD3 ='01'. APPEND WA TO IT.

WA-FIELD1 = 'P'.WA-FIELD2 = 'x'.WA-FIELD3 ='01'. APPEND WA TO IT.

WA-FIELD1 = 'D'.WA-FIELD2 = 'b'.WA-FIELD3 ='01'. APPEND WA TO IT."Delete

WA-FIELD1 = 'C'.WA-FIELD2 = 'b'.WA-FIELD3 ='02'. APPEND WA TO IT.

WA-FIELD1 = 'B'.WA-FIELD2 = 'b'.WA-FIELD3 ='01'. APPEND WA TO IT."Delete

WA-FIELD1 = 'P'.WA-FIELD2 = 'x'.WA-FIELD3 ='02'. APPEND WA TO IT.

SORT IT ASCENDING.

*APPEND LINES OF IT[] TO IT1[].

*WRITE:/'Before deleting Unique records'.

*WRITE:/'------------------------------'.

*

*LOOP AT IT[] INTO WA.

*  WRITE:/ WA-FIELD1,WA-FIELD2,WA-FIELD3.

*ENDLOOP.

DELETE ADJACENT DUPLICATES FROM IT[] COMPARING ALL FIELDS.

*DELETE ADJACENT DUPLICATES FROM IT[] COMPARING FIELD1 FIELD2.

LOOP AT IT[] INTO WA1.

  CLEAR COUNT.

  LOOP AT IT[] INTO WA WHERE FIELD1 EQ WA1-FIELD1 AND FIELD2 EQ WA1-FIELD2 AND FIELD3 NE WA1-FIELD3. "Fields to compare uniqueness

    ADD 1 TO COUNT.

    if COUNT EQ 1.

      APPEND WA1 TO IT1.

     ENDIF.

*   CHECK COUNT EQ 2.

*   EXIT.

     APPEND WA TO IT1.

   ENDLOOP.

*  CHECK COUNT GE 1.

*  DELETE IT[] WHERE FIELD1 EQ WA1-FIELD1 AND FIELD2 EQ WA-FIELD2.

   IF COUNT GE 1.

*     APPEND WA1 TO IT1.

     DELETE IT[] WHERE FIELD1 EQ WA1-FIELD1 AND FIELD2 EQ WA1-FIELD2 AND FIELD3 NE WA1-FIELD3.

   ENDIF.

ENDLOOP.

WRITE:/'After filtering'.

WRITE:/'------------------------------'.

LOOP AT IT1[] INTO WA1.

   WRITE:/ WA1-FIELD1,WA1-FIELD2,WA1-FIELD3.

ENDLOOP.

According to you is correct?

For

Former Member
0 Kudos
1,097

Hi,

You have to use the control break statements.

Former Member
0 Kudos
1,097

Hi Tony,

Will you tell me what about 4th column (Pos) stands for.

i'm asking for logical point of view.

if you have pos = 1 for each same line item that means there is no any duplicate.

if you found pos more then 1 with the line items , which means you have duplicate values somewhere.

please clarify .

thanks .

Avirat

0 Kudos
1,097

Hi Patel,

I try to explain better:

This table contain the quotation value of material (quotation means economic value of material).

For this motivation isn´t possible to have for the same Mandt (customer) same Qnr and same material

two or more differents Pos, because the system doesn´t know which one take.

Mandt       Qnr         Material            Pos

010         000526        0019232           01

010         000526        0019232           02

isn´t correct

while this is correct

Mandt       Qnr         Material            Pos

010         000526        0019232           01

010         000526        0019231           02

I can find the record not correct.

0 Kudos
1,097

for your above code with it1 and it as internal tables :
you can use this logic

SORT it ASCENDING.
DATA: lv_count TYPE int4.

LOOP AT it[] INTO wa1.
    AT END OF field3.

      APPEND wa1 TO it1.

  ENDAT.
ENDLOOP.

LOOP AT it1[] into wa1.
   lv_count = lv_count + 1.
   AT END OF field2.
     IF lv_count = 1.
       DELETE it1 INDEX sy-tabix.
     ENDIF.
     CLEAR lv_count.
   ENDAT.
ENDLOOP.

This will show you the erroneous records you want to display. Hope this helps.

0 Kudos
1,097

Hi Maverick,

It´s perfect I try to execute and this find exactly the error records.

One thing that I didn´t understand, the statement

AT END OF

ENDAT

How works??

I didn´t understand the value of field3.

Thank you

0 Kudos
1,097

Hi Tony ,
              AT END OF is a loop event . I will try to explain you this one.
AT END  OF field3  - If your internal table contains 4 fields define in the order
field1
field2
field3

field4
the event will be triggered  or the code between will be executed during the last loop pass and every time the value of the control level (field3) changes or any other field left to the control level changes

for example you have record
1     1     1     1
1     1     1     2

1     1     2     1

1     2     2     1

when the loop will run for the first time the code inside AT END OF .. END AT will not be executed as you can see the second records contains same value of field1 field2 field3 .
when the loop will run for the second time  this time the code will be executed inside as the mext record which is going to come inside loop has a change in field3 value .
when the loop will run for the third time this time too the code will be executed because there is a change in field2 even if no change in field3 is going to be in the next record.
So if you say AT END OF filed3
system will take the combination of field1 field2 field3 and if any thing changes it will trigger the code.  Field1 and field 2 should have been defined in intenal table before field3.

I hope you get some idea , otherwise there are many tutorials on loop events in ABAP you can check them on net ....

0 Kudos
1,097

OK Maverick,

your explanation it was very useful thank you so much.

Do you think that the solution I developed using the following loop

LOOP AT IT[] INTO WA1.

  CLEAR COUNT.

  LOOP AT IT[] INTO WA WHERE FIELD1 EQ WA1-FIELD1 AND FIELD2 EQ WA1-FIELD2 ANDFIELD3 NE WA1-FIELD3. "Fields to compare uniqueness

    ADD 1 TO COUNT.

    if COUNT EQ 1.

      APPEND WA1 TO IT1.

     ENDIF.

*   CHECK COUNT EQ 2.

*   EXIT.

     APPEND WA TO IT1.

   ENDLOOP.

*  CHECK COUNT GE 1.

*  DELETE IT[] WHERE FIELD1 EQ WA1-FIELD1 AND FIELD2 EQ WA-FIELD2.

   IF COUNT GE 1.

*     APPEND WA1 TO IT1.

     DELETE IT[] WHERE FIELD1 EQ WA1-FIELD1 AND FIELD2 EQ WA1-FIELD2 AND FIELD3NE WA1-FIELD3.

   ENDIF.

ENDLOOP.

is also a good solution, or No?

0 Kudos
1,097

Hi Tony ,
           Your logic is fine, i think it should work too , and any logic that works and is developed by your ownself is always a good solution,  . In abap we prefer not to use LOOP inside LOOP until its unavoidable .

0 Kudos
1,097

Hi at all,

I contact you always for the same problem. I try to execute my program, but the problem is that the table

where the program work has approx 7.3 milion of record, and using two loop isn´t a good idea because the program after more than one day the program hasn´t finish to work.

I try to modify my code and now I´m using the following code:


SELECT * FROM /wst/sqitem INTO TABLE i_sqitem

             WHERE matfrom NE space.

   IF sy-subrc = 0.

     SELECT * FROM /wst/sqitem

           INTO CORRESPONDING FIELDS OF TABLE i_sqitem1

           FOR ALL ENTRIES IN i_sqitem

           WHERE qnr EQ i_sqitem-qnr AND

                 matfrom  EQ i_sqitem-matfrom AND

                 posnr NE i_sqitem-posnr.

     SORT i_sqitem1 BY qnr matfrom .

   ENDIF.

Where  i_sqitem1 and i_sqitem are internal table and  /wst/sqitem the table that contain this recod.

I would remember that my problem:

I have this table

Mandt          Quot.no.                      Pos.          Material from

010300003159031710    005  100
0103000031510031710    005 
0103000031511031710    005  100
010300003158031710    005  100
01030000315407032800  061    1
010300003151307032800  061    1
010300003151407032830
010300003151507032833
01030000315307032800  061    1

My result it should be this

010300003159031710    005  100
0103000031511031710    005  100
010300003158031710    005  100
01030000315407032800  061    1
010300003151307032800  061    1
01030000315307032800  061    1

Do you think now my program is more powerful? I should not have more than the same problems on a table so big?

Thank you in advance


Tony

Former Member
0 Kudos
1,097

Hi Tony

Refering back to your original post, this can be done in just 3 lines of code:

SORT <internal_table> BY MANDT QNR MATERIAL.

<internal_table_new>[] = <internal_table>[].

DELETE ADJACENT DUPLICATES FROM <internal_table> COMPARING MANDT QNR MATERIAL.

That is all the code you should need.

Regards

Daren