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: 

Problem with displaying duplicate entries in an internal table

babu_kilari4
Active Contributor
0 Kudos

Hi,

I have requirment to display only the duplicate entries in an internal table.

I will give an example scenario to explain this.

I have an internal table with name say t_xyz

In this internal table I have few fields say a b c d e

Data in the internal table is shown below

a b c d e

1 1 2 3 4

1 1 3 4 5

1 2 3 4 6

1 3 4 4 6

2 3 4 5 6

2 3 4 7 8

If I select the fields a and b, I have to display the possible duplicate records for those two fields in the output as shown below

1 1 2 3 4

1 1 3 4 5

2 3 4 5 6

2 3 4 7 8

Similary if i select some other two fields, I have to display the possible duplicate records for those two fields.

Finally the main requirment is, I dont want to transfer the contents in to a new internal table.

I want to have the same internal table,but I wanted to delete the entries which are not duplicates leaving all the duplicate entries.

Can any one give me best possible solution to achieve this scenario.

1 ACCEPTED SOLUTION

former_member188829
Active Contributor
0 Kudos

Hi Babu,

Check this Program.

DATA:BEGIN OF itab1 OCCURS 0,
     a TYPE c,
     b TYPE c,
     c TYPE c,
     d TYPE c,
     e TYPE c,
     END OF itab1,

     BEGIN OF itab2 OCCURS 0,
     a TYPE c,
     b TYPE c,
     c TYPE c,
     d TYPE c,
     e TYPE c,
     END OF itab2,

     BEGIN OF itab3 OCCURS 0,
     a TYPE c,
     b TYPE c,
     c TYPE c,
     d TYPE c,
     e TYPE c,
     END OF itab3,

     BEGIN OF itab OCCURS 0,
     a TYPE c,
     b TYPE c,
     c TYPE c,
     d TYPE c,
     e TYPE c,
     END OF itab.

START-OF-SELECTION.
  itab1-a = 1.
  itab1-b = 1.
  itab1-c = 2.
  itab1-d = 3.
  itab1-e = 4.
  APPEND itab1.

  itab1-a = 1.
  itab1-b = 1.
  itab1-c = 3.
  itab1-d = 4.
  itab1-e = 5.
  APPEND itab1.

  itab1-a = 1.
  itab1-b = 2.
  itab1-c = 3.
  itab1-d = 4.
  itab1-e = 6.
  APPEND itab1.

  itab1-a = 1.
  itab1-b = 3.
  itab1-c = 4.
  itab1-d = 4.
  itab1-e = 6.
  APPEND itab1.

  itab1-a = 2.
  itab1-b = 3.
  itab1-c = 4.
  itab1-d = 5.
  itab1-e = 6.
  APPEND itab1.

  itab1-a = 2.
  itab1-b = 3.
  itab1-c = 4.
  itab1-d = 7.
  itab1-e = 8.
  APPEND itab1.

  SORT itab1.

  LOOP AT itab1.
    AT NEW b.
      READ TABLE itab1 INDEX sy-tabix.
      MOVE itab1 TO itab2.
      APPEND itab2.
    ENDAT.
  ENDLOOP.

  LOOP AT itab1.
    AT END OF b.
      READ TABLE itab1 INDEX sy-tabix.
      MOVE itab1 TO itab3.
      APPEND itab3.
    ENDAT.
  ENDLOOP.

  LOOP AT itab2.
    LOOP AT itab3 WHERE a = itab2-a AND b = itab2-b AND c = itab2-c
                        AND d = itab2-d AND e = itab2-e .
      DELETE itab2.
      DELETE itab3.
    ENDLOOP.
  ENDLOOP.

  LOOP AT itab2.
    MOVE itab2 TO itab.
    APPEND itab.
  ENDLOOP.

  LOOP AT itab3.
    MOVE itab3 TO itab.
    APPEND itab.
  ENDLOOP.
  SORT itab.
  LOOP AT itab.
    WRITE:/ itab-a,itab-b,itab-c,itab-d,itab-e.
  ENDLOOP.

9 REPLIES 9

former_member226203
Active Contributor
0 Kudos

try this:

first LOOP on the internal table into WA.

*if u want to have the values for a, b

declare a TEMP table of the same type.

check each time inside the loop with the current entries in the wa and the entries in the TEMP table.

if the entries are found repeated don make any chage to the temp table.

else if entires are not repeated delete that frm TEMP.

now,

READ the internal table with key fields a,b into a wa.

append this to an internal tale of the same type TEMP.

OR.

u can use AT NEW statement here to chekc for the duplicate entries.

ENDLOOP.

MODIFY ur internal table from TEMP.

Former Member
0 Kudos

Hi,

let

p_a, p_b, p_c p_d p_e

be the checkboxes for selecting internal table fields out of which only 2 can be selectd

declare field symbol like your internal table field

1) field-symbols: <fs1> like (your internal table field).

field-symbols: <fs2> like (your internal table field).

case 'X'.

when p_a.

field1 = 't_xyz-a'

clear p_a

when p_b

field1 = 't_xyz-b'

clear p_b

when p_c

field1 = 'txyz-c'

clear p_c

when p_d

field1 = 't_xyz-d'

clear p_d

when p_e

field1 = 't_xyz-e'

clear p_e

when others

endcase.

case 'X'.

when p_a.

field2='t_xyz-a'

when p_b

field2= 't_xyz-b'

when p_c

field2= 't_xyz-c'

when p_d

field2= 't_xyz-d'

when p_e

field2= 't_xyz-e'

when others

endcase.

now, if a and b are to be compared, field1 will have 't_xyz-a' and field2 will have 't_xyz-b'

loop at t_xyz.

assign (field1) to <fs1>.

assign (field2) to <fs2>.

if <fs1> eq old1 and <fs2> eq old2

" write code to display in output

endif.

old1 = <fs1>

old2 = <fs2>

endloop.

Regards,

Arun

Edited by: Arun Gangadharan on Mar 25, 2009 6:19 AM

Edited by: Arun Gangadharan on Mar 25, 2009 6:28 AM

former_member188829
Active Contributor
0 Kudos

Hi Babu,

Check this Program.

DATA:BEGIN OF itab1 OCCURS 0,
     a TYPE c,
     b TYPE c,
     c TYPE c,
     d TYPE c,
     e TYPE c,
     END OF itab1,

     BEGIN OF itab2 OCCURS 0,
     a TYPE c,
     b TYPE c,
     c TYPE c,
     d TYPE c,
     e TYPE c,
     END OF itab2,

     BEGIN OF itab3 OCCURS 0,
     a TYPE c,
     b TYPE c,
     c TYPE c,
     d TYPE c,
     e TYPE c,
     END OF itab3,

     BEGIN OF itab OCCURS 0,
     a TYPE c,
     b TYPE c,
     c TYPE c,
     d TYPE c,
     e TYPE c,
     END OF itab.

START-OF-SELECTION.
  itab1-a = 1.
  itab1-b = 1.
  itab1-c = 2.
  itab1-d = 3.
  itab1-e = 4.
  APPEND itab1.

  itab1-a = 1.
  itab1-b = 1.
  itab1-c = 3.
  itab1-d = 4.
  itab1-e = 5.
  APPEND itab1.

  itab1-a = 1.
  itab1-b = 2.
  itab1-c = 3.
  itab1-d = 4.
  itab1-e = 6.
  APPEND itab1.

  itab1-a = 1.
  itab1-b = 3.
  itab1-c = 4.
  itab1-d = 4.
  itab1-e = 6.
  APPEND itab1.

  itab1-a = 2.
  itab1-b = 3.
  itab1-c = 4.
  itab1-d = 5.
  itab1-e = 6.
  APPEND itab1.

  itab1-a = 2.
  itab1-b = 3.
  itab1-c = 4.
  itab1-d = 7.
  itab1-e = 8.
  APPEND itab1.

  SORT itab1.

  LOOP AT itab1.
    AT NEW b.
      READ TABLE itab1 INDEX sy-tabix.
      MOVE itab1 TO itab2.
      APPEND itab2.
    ENDAT.
  ENDLOOP.

  LOOP AT itab1.
    AT END OF b.
      READ TABLE itab1 INDEX sy-tabix.
      MOVE itab1 TO itab3.
      APPEND itab3.
    ENDAT.
  ENDLOOP.

  LOOP AT itab2.
    LOOP AT itab3 WHERE a = itab2-a AND b = itab2-b AND c = itab2-c
                        AND d = itab2-d AND e = itab2-e .
      DELETE itab2.
      DELETE itab3.
    ENDLOOP.
  ENDLOOP.

  LOOP AT itab2.
    MOVE itab2 TO itab.
    APPEND itab.
  ENDLOOP.

  LOOP AT itab3.
    MOVE itab3 TO itab.
    APPEND itab.
  ENDLOOP.
  SORT itab.
  LOOP AT itab.
    WRITE:/ itab-a,itab-b,itab-c,itab-d,itab-e.
  ENDLOOP.

0 Kudos

Vishnu,

Your example code works fine.

But, dont you think it becomes a performance issue we are going to use more internal tables.

I am trying to restrict the code with the same internal table which has data.

And I want to pass this internal table with the duplicate values to the ALV to display the data.

I dont want to use one more table. All I need is to manipulate with only one table by deleting the non-duplicate records.

0 Kudos

Hi Babu,

This is the idea which i got when i was doing. I will try one more time and let you know for getting performance and display the data in the same internal table itself.

0 Kudos

Thanks Vishnu,

Also adding to the above, it doesnt mean that I always select the fields a and b.

It can be a and c or a and d or b and d........

This is the main problem. Any pointers on this would be highly appreciated.

I really do not want to rewrite the same code for each set of selections.

Note: Selecting the fields would be on Selection Screen.

0 Kudos

Hi Babu,

see my post above. Here for every field, if you have a checkbox on the selection screen, and if you implement the logic as below, you will not have to rewrite the code for every condition again. I am pasting the same here since i could not edit the earlier post

Let

p_a, p_b, p_c, p_d, p_e

be the checkboxes for selecting internal table fields out of which only 2 can be selectd

declare field symbol like your internal table field

1) field-symbols: <fs1> like (your internal table field).

field-symbols: <fs2> like (your internal table field).

case 'X'.

when p_a.

field1 = 'T_XYZ_A'

clear p_a

when p_b

field1 = 'T_XYZ_B'

clear p_b

when p_c

field1 = 'T_XYZ_C'

clear p_c

when p_d

field1 = 'T_XYZ_D'

clear p_d

when p_e

field1 = 'T_XYZ_E'

clear p_e

when others

endcase.

case 'X'.

when p_a.

field2='T_XYZ_A'

when p_b

field2= 'T_XYZ_B'

when p_c

field2= 'T_XYZ_C'

when p_d

field2= 'T_XYZ_D'

when p_e

field2= 'T_XYZ_E'

when others

endcase.

now, if a and b are to be compared, field1 will have 't_xyz-a' and field2 will have 't_xyz-b'

loop at t_xyz.

assign (field1) to <fs1>.

assign (field2) to <fs2>.

if <fs1> eq old1 and <fs2> eq old2

" write code to display in output

endif.

old1 = <fs1>

old2 = <fs2>

endloop.

Regards,

Arun

Regards,

Arun

0 Kudos

Hi Arun,

Actually your code looks great. Thanks a lot for the input.

I saw your code already and I was expecting any answer which doesn't use field symbols concept.

But, thanks a lot for the effort.

Former Member
0 Kudos

Try this sample code:

Sort t_xyz by a b.

Loop at t_xyz

At new b.

Clear cnt.

Cnt = 0.

Endat.

If cnt = 1.

Write: temp_xyz-a to till temp_xyz-e

Clear temp_xyz.

Write: t_xyz-a to till t_xyz-e

Elseif cnt gt 1

Write: t_xyz-a to till t_xyz-e

else.

Clear temp_xyz.

Temp_xyz = t_xyz

Endif.

Cnt = cnt + 1

Endloop.