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: 

Removing duplicates in the Internal Table

Mula
Associate
Associate
0 Kudos

Dear friends,

Could any one of you kindly help me with a code to delete the duplicates in the internal table, but each duplicate should be counted, how many times that appeared and should be displayed again as a report with the messages and no of times that message appeared.

Thank you,

Best Regards,

subramanyeshwer

1 ACCEPTED SOLUTION

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

You can try something like this.



report zrich_0001.


data: begin of itab1 occurs 0,
      fld1 type c,
      fld2 type c,
      fld3 type c,
      end of itab1.

data: begin of itab2 occurs 0,
      fld1 type c,
      fld2 type c,
      fld3 type c,
      end of itab2.

data: counter type i.

itab1 = 'ABC'.  append itab1.
itab1 = 'DEF'.  append itab1.
itab1 = 'GHI'.  append itab1.
itab1 = 'DEF'.  append itab1.
itab1 = 'GHI'.  append itab1.
itab1 = 'DEF'.  append itab1.


itab2[] = itab1[].

sort itab1 ascending.
delete adjacent duplicates from itab1.

loop at itab1.

clear counter.
  loop at itab2 where fld1 = itab1-fld1
                 and  fld2 = itab1-fld2
                 and  fld3 = itab1-fld3.
    counter = counter + 1.
  endloop.

 write:/ itab1-fld1, itab1-fld2, itab1-fld3,
         'Number of occurances:', counter.

endloop.


Regards,

Rich Heilman

12 REPLIES 12

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

You can try something like this.



report zrich_0001.


data: begin of itab1 occurs 0,
      fld1 type c,
      fld2 type c,
      fld3 type c,
      end of itab1.

data: begin of itab2 occurs 0,
      fld1 type c,
      fld2 type c,
      fld3 type c,
      end of itab2.

data: counter type i.

itab1 = 'ABC'.  append itab1.
itab1 = 'DEF'.  append itab1.
itab1 = 'GHI'.  append itab1.
itab1 = 'DEF'.  append itab1.
itab1 = 'GHI'.  append itab1.
itab1 = 'DEF'.  append itab1.


itab2[] = itab1[].

sort itab1 ascending.
delete adjacent duplicates from itab1.

loop at itab1.

clear counter.
  loop at itab2 where fld1 = itab1-fld1
                 and  fld2 = itab1-fld2
                 and  fld3 = itab1-fld3.
    counter = counter + 1.
  endloop.

 write:/ itab1-fld1, itab1-fld2, itab1-fld3,
         'Number of occurances:', counter.

endloop.


Regards,

Rich Heilman

0 Kudos

Hello Rich,

some how its not looping pefectly(inner loop).

I am getting the counter as 1 always.

I could not understand why.

Could you please help.

thank you,

Best Regards,

Subramanyeshwer

0 Kudos

Are you working with my sample program, or your program. If your program, post the code, so I can take a look. My sample above works quite well in my system.

Regards,

Rich Heilman

0 Kudos

Try this. Catch is that you should not have any number type fields in your internal table.


DATA: BEGIN OF itab1 OCCURS 0,
        fld1 TYPE c,
        fld2 TYPE c,
        fld3 TYPE c.
DATA: END OF itab1.

DATA: BEGIN OF itab2 OCCURS 0,
        fld1  TYPE c,
        fld2  TYPE c,
        fld3  TYPE c,
        count TYPE i.
DATA: END OF itab2.

itab1 = 'ABC'.  APPEND itab1.
itab1 = 'DEF'.  APPEND itab1.
itab1 = 'GHI'.  APPEND itab1.
itab1 = 'DEF'.  APPEND itab1.
itab1 = 'GHI'.  APPEND itab1.
itab1 = 'DEF'.  APPEND itab1.

LOOP AT itab1.
  MOVE: itab1-fld1 TO itab2-fld1,
        itab1-fld2 TO itab2-fld2,
        itab1-fld3 TO itab2-fld3,
        1          TO itab2-count.
  COLLECT itab2.
  CLEAR itab2.
ENDLOOP.

LOOP AT itab2.
  WRITE:/ itab2-fld1,
          itab2-fld2,
          itab2-fld3,
          itab2-count.
ENDLOOP.

0 Kudos

Hello Rich,

Perfect.

The problem is solved with the example you provided.

Thanks a lot and I checked the radio button "Problem solved".

Regards,

subramanyeshwer

Former Member
0 Kudos

--say your internal table imsg has the field msgkey data.

sort imsg.

loop at imsg.

lcnt = lcnt + 1.

at end of msgkey.

write : / imsg-msgkey, lcnt.

*you can even update it into another internal table and print later.

endat.

endloop.

delete adjacent duplicates from imsg.

*Please check the exact syntax.

--Did you try the above code it should definetly work..

Message was edited by: Anurag Bankley

Former Member
0 Kudos

Hi,

sort itab by fieldname.

delete adjacent duplicates from itab comparing fieldname>.

will work fine.

Regards,

Sowjanya.

Message was edited by: sowjanya suggula

0 Kudos

Hello Sowjanya,

Thank you for your reply.

I could able to sort and delete the duplicates. but the point is how display that how many duplicates were present. I am trying to get the count of duplicates.

Thank you,

Best Regards,

Subramanyeshwer

Former Member
0 Kudos

this will give u the no. of duplicates of table t_t with fields f1, f2.

loop at t_t.

at new f2.

v1 = sy-tabix.

endat.

at end of f2.

v2 = sy-tabix.

read table t_t index v2.

v_c = v2 - v1 + 1.

write:/ t_t-f1, space, t_t-f2, v_c.

endat.

endloop.

then delete duplicates.

-Anu

0 Kudos

Hello Anu,

Thank you for the reply.

Could you please eloborate a bit.

I could not understand the varialbles and flow of the code.

Thank you,

Best Regards,

Subramanyeshwer

Former Member
0 Kudos

Hi,

Rich's code is correct...

In debugging, You can check-out if all the individual fields are having the same values in itab1 and itab2 for the counter to get incremented.

Regards,

Raj

0 Kudos

Hello Raj,

your reply forced me to check the program perfectly.

Thank you for your reply.

I checked the radio button "Very helpfull answer".

Regards,

Subramanyeshwer