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: 

issue with deleting adjacent duplicates from internal tab

0 Kudos

Hi eveyrone. Its been some years the last time I did some abap coding so I am coming a bit rusty already.

I have dilemma how to delete duplicates in my internal table. For example I have the following entries in the internal table

Fld1        Field2          Field3          Field4

1          march1          A602          A602 description

1          march1          A610          A610 description

2          march 1         A602          A602 description

I need to delete duplicates in the itab above. But I need to retain record#2 and record#3. I need to retain the last instance of the duplicates and not the first one. Please help.

Thanks.

G

1 ACCEPTED SOLUTION

jogeswararao_kavala
Active Contributor
0 Kudos

Use Sort by Descending Order.

SORT IT_XYZ BY FLD1 FIELD2 FIELD3 DESCENDING.

This puts the records of internal table in this order

Fld1     Field2             Field3        Field4

2          march1          A602          A602 description

1          march1          A602          A602 description

1          march1          A610          A610 description

And then use this

DELETE ADJACENT DUPLICATES FROM IT_XYZ COMPARING FIELD2 FIELD3 FIELD4.

This will remove middle record and retain these two records

Fld1     Field2             Field3        Field4

2          march1          A602          A602 description

1          march1          A610          A610 description

KJogeswaraRao

4 REPLIES 4

jogeswararao_kavala
Active Contributor
0 Kudos

Use Sort by Descending Order.

SORT IT_XYZ BY FLD1 FIELD2 FIELD3 DESCENDING.

This puts the records of internal table in this order

Fld1     Field2             Field3        Field4

2          march1          A602          A602 description

1          march1          A602          A602 description

1          march1          A610          A610 description

And then use this

DELETE ADJACENT DUPLICATES FROM IT_XYZ COMPARING FIELD2 FIELD3 FIELD4.

This will remove middle record and retain these two records

Fld1     Field2             Field3        Field4

2          march1          A602          A602 description

1          march1          A610          A610 description

KJogeswaraRao

raphael_almeida
Active Contributor
0 Kudos

Hi dgrachee!

Adding to that Jogeswara said, you can also create an internal table as sorted (TYPE SORTED TABLE) and by the sql query (if an internal table used in SQL statements) and order the query as descending, as I show below:

TYPES: BEGIN OF y_spfli,
          carrid TYPE spfli-carrid,
          connid TYPE spfli-connid,
        END OF y_spfli.

DATE t_spfli TYPE SORTED TABLE OF y_spfli WITH NON-UNIQUE KEY carrid connid.

SELECT carrid connid
   FROM spfli
   INTO TABLE t_spfli
   UP TO 50 ROWS
   ORDER BY connid DESCENDING.

0 Kudos

You can't do this if CONNID is part of the key of the SORTED table. Either the ORDER BY will be ignored or you'll get a dump.

Perhaps you mean:

TYPES: BEGIN OF y_spfli,

          carrid TYPE spfli-carrid,

          connid TYPE spfli-connid,

        END OF y_spfli.

DATE t_spfli TYPE SORTED TABLE OF y_spfli WITH NON-UNIQUE KEY carrid.


SELECT carrid connid

   FROM spfli

   INTO TABLE t_spfli

   UP TO 50 ROWS

   ORDER BY connid DESCENDING.

0 Kudos

Master Billingham teach me again 😛

Thanks for pointing my mistake.


BR,