03-13-2015 8:08 PM
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
03-13-2015 10:04 PM
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
03-13-2015 10:04 PM
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
03-15-2015 3:26 AM
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.
03-16-2015 5:58 AM
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.
03-16-2015 12:15 PM
Master Billingham teach me again 😛
Thanks for pointing my mistake.
BR,