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: 

Delete

Former Member
0 Kudos
127

Hi,

How to delete duplicates from internal tables..

plz help.

martina.

1 ACCEPTED SOLUTION

Former Member
0 Kudos
106

Hi martina,

you can use,

DELETE ADJACENT DUPLICATES FROM itab.

Additions

1. ... COMPARING f1 f2 ...

2. ... COMPARING ALL FIELDS

Effect

Deletes neighboring, duplicate entries from the internal table itab . If there are n duplicate entries, the first entry is retained and the other n - 1 entries are deleted.

Two lines are considered to be duplicated if their default keys match.

The return code value is set as follows:

SY-SUBRC = 0 At least one duplicate exists, at least one entry deleted.

SY_SUBRC = 4 No duplicates exist, no entry deleted.

Addition 1

... COMPARING f1 f2 ...

Effect

Two lines of the internal table itab are considered to be duplicates if the specified fields f1 , f2 , .... match.

Addition 2

... COMPARING ALL FIELDS

Effect

Two lines are considered to be duplicates if all fields of the table entries match.

Notes

The DELETE ADJACENT DUPLICATES statement is especially useful if the internal table itab is sorted by fields (whether in ascending or descending order) which were compared during duplicate determination. In this case, the deletion of neighbouring duplicates is the same as the deletion of all duplicates.

If a comparison criterion is only known at runtime, it can be specified dynamically as the content of a field name by using COMPARING ... (name) ... . If name is blank at runtime, the comparison criterion is ignored. If name contains an invalid component name, a runtime error occurs.

Comparison criteria - statistically or dynamically specified - can be further restriced by specifying the offset and/or length.

Note

Performance

Deleting a line from an internal table incurs index maintenance costs which depend on the index of the line to be deleted. The runtime depends on the line width of the table.

For example, deleting a line in the middle of an internal table with 200 entries requires about 10 msn (standardized microseconds).

Deleting a range of entries with " DELETE itab FROM idx1 TO idx2. " deleting a set of entries with " DELETE itab WHERE ... " only incur index maintenance costs once. Compared with a LOOP , which deletes line-by-line, this is much faster.

To delete neighboring, duplicate entries from an internal table, use the variant " DELETE ADJACENT DUPLICATES FROM itab. " instead of LOOP constructions.

Reward points if helpful !

Regards,

Vrushali

8 REPLIES 8

Former Member
0 Kudos
106

HI,

DELETE ADJUCENT DUPLICATES FROM <internal table> COMPARING ALL FIELDS.

THANKS,CSR

0 Kudos
106

First <b>SORT</b> your internal table

<b>DELETE ADJUCENT DUPLICATES FROM</b> <internal table> <b>COMPARING ALL FIELDS</b>

Regards,

SaiRam

Former Member
0 Kudos
106

sort and delete using the below example

DELETE ADJACENT DUPLICATES FROM f_itab2 COMPARING ebeln.

Former Member
0 Kudos
106

Hi Martina,

first sort your internal table.

delete adjacent duplicates from <internal table> comparing <field name>.

Example.

tables: pa0000.

*-- Internal table for PA0000 data

DATA: BEGIN OF it_0000 OCCURS 0,

pernr TYPE persno,

endda TYPE endda,

begda TYPE begda,

stat2 TYPE stat2,

END OF it_0000.

select-options: s_pernr for pa0000-pernr.

  • Get data from infotype 0000

SELECT pernr endda begda stat2 FROM pa0000

INTO TABLE it_0000

WHERE pernr IN s_pernr

AND stat2 EQ '3'.

IF sy-subrc = 0.

SORT it_0000 BY pernr begda DESCENDING.

DELETE ADJACENT DUPLICATES FROM it_0000 COMPARING pernr.

ENDIF.

Message was edited by:

Velangini Showry Maria Kumar Bandanadham

Former Member
0 Kudos
106

first sort the internal table and then use DELETE ADJACENT DUPLICATE ENTRIES.

Former Member
0 Kudos
106

Hello,

Use

First sort the ITAB

Then

DELETE ADJACENT DUPLICATES FROM ITAB

Reward if helpful

Regards,

LIJO

Former Member
0 Kudos
106

Hi,

Syntax

DELETE ADJACENT DUPLICATE ENTRIES FROM <itab> [COMPARING... ].

Deletes adjacent duplicate entries, either by comparing the key fields or the comparison fields specified explicitly in the COMPARING addition.

Ex.

DATA OFF TYPE I.

DATA: BEGIN OF LINE,

COL1 TYPE I,

COL2 TYPE C,

END OF LINE.

DATA ITAB LIKE STANDARD TABLE OF LINE

WITH NON-UNIQUE KEY COL2.

LINE-COL1 = 1. LINE-COL2 = 'A'. APPEND LINE TO ITAB.

LINE-COL1 = 1. LINE-COL2 = 'A'. APPEND LINE TO ITAB.

LINE-COL1 = 1. LINE-COL2 = 'B'. APPEND LINE TO ITAB.

LINE-COL1 = 2. LINE-COL2 = 'B'. APPEND LINE TO ITAB.

LINE-COL1 = 3. LINE-COL2 = 'B'. APPEND LINE TO ITAB.

LINE-COL1 = 4. LINE-COL2 = 'B'. APPEND LINE TO ITAB.

LINE-COL1 = 5. LINE-COL2 = 'A'. APPEND LINE TO ITAB.

OFF = 0. PERFORM LIST.

DELETE ADJACENT DUPLICATES FROM ITAB COMPARING ALL FIELDS.

OFF = 14. PERFORM LIST.

DELETE ADJACENT DUPLICATES FROM ITAB COMPARING COL1.

OFF = 28. PERFORM LIST.

DELETE ADJACENT DUPLICATES FROM ITAB.

OFF = 42. PERFORM LIST.

FORM LIST.

SKIP TO LINE 3.

LOOP AT ITAB INTO LINE.

WRITE: AT /OFF LINE-COL1, LINE-COL2.

ENDLOOP.

ENDFORM.

Regards,

Bhaskar

Former Member
0 Kudos
107

Hi martina,

you can use,

DELETE ADJACENT DUPLICATES FROM itab.

Additions

1. ... COMPARING f1 f2 ...

2. ... COMPARING ALL FIELDS

Effect

Deletes neighboring, duplicate entries from the internal table itab . If there are n duplicate entries, the first entry is retained and the other n - 1 entries are deleted.

Two lines are considered to be duplicated if their default keys match.

The return code value is set as follows:

SY-SUBRC = 0 At least one duplicate exists, at least one entry deleted.

SY_SUBRC = 4 No duplicates exist, no entry deleted.

Addition 1

... COMPARING f1 f2 ...

Effect

Two lines of the internal table itab are considered to be duplicates if the specified fields f1 , f2 , .... match.

Addition 2

... COMPARING ALL FIELDS

Effect

Two lines are considered to be duplicates if all fields of the table entries match.

Notes

The DELETE ADJACENT DUPLICATES statement is especially useful if the internal table itab is sorted by fields (whether in ascending or descending order) which were compared during duplicate determination. In this case, the deletion of neighbouring duplicates is the same as the deletion of all duplicates.

If a comparison criterion is only known at runtime, it can be specified dynamically as the content of a field name by using COMPARING ... (name) ... . If name is blank at runtime, the comparison criterion is ignored. If name contains an invalid component name, a runtime error occurs.

Comparison criteria - statistically or dynamically specified - can be further restriced by specifying the offset and/or length.

Note

Performance

Deleting a line from an internal table incurs index maintenance costs which depend on the index of the line to be deleted. The runtime depends on the line width of the table.

For example, deleting a line in the middle of an internal table with 200 entries requires about 10 msn (standardized microseconds).

Deleting a range of entries with " DELETE itab FROM idx1 TO idx2. " deleting a set of entries with " DELETE itab WHERE ... " only incur index maintenance costs once. Compared with a LOOP , which deletes line-by-line, this is much faster.

To delete neighboring, duplicate entries from an internal table, use the variant " DELETE ADJACENT DUPLICATES FROM itab. " instead of LOOP constructions.

Reward points if helpful !

Regards,

Vrushali