Application Development and Automation 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: 
Read only

DELETE ADJACENT DUPLICATES compering

Former Member
0 Likes
76,529

hi,

i have table with 3 key fields and i wont to delete rows in table

where the key fields are the same .

what is the best way to do that?

e.g.

lets say key fields is pernr orgeh name i.d

adress & city not

i have:

pernr----orgeh


name-i.d
address
-- city

123-----5008855-ferry -


5122285usa---new york

123-----5008855---ferry -


5122285

123-----5008855---ferry -


5122285

i wont after delete just one recored :

pernr----orgeh


name-i.d
address
-- city

123-----5008855-ferry -


5122285usa---new york

Regards

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
27,787

hi,

DELETE ADJACENT DUPLICATES FROM <itab>

[COMPARING <f1> .. <fn>|ALL FIEL <fn>}|ALL FIELDS}].

The system deletes all adjacent entries with the same key field contents apart from the first entry. You

can prevent the system from only comparing the key field using the COMPARING addition. If you sort

the table by the required fields beforehand, you can be sure that only unique entries will remain in the

table after the DELETE ADJACENT DUPLICATES statement.

Hope this helps, Do reward.

10 REPLIES 10
Read only

Former Member
0 Likes
27,787

Hi,

If in the table first 3 fields are key fields then you wont be able to enter duplicate entries in the table.

thanks and Regards

Sowmmya VB

Read only

Former Member
0 Likes
27,787

Hi Ricardo,

Try this,

sort itab by pernr orgeh name.

Delete adjacent duplicates from itab comparing pernr orgeh name .

This comparing fields should be in the order of priority.

Regards,

Chitra

Read only

Former Member
0 Likes
27,787

Hi

First sort your ITAB

SORT ITAB by <key-field1> <key-field2> <key-field3>.

delect adjacent duplicates from ITAB.

Regards

Aditya

Read only

Former Member
0 Likes
27,787

hi,

using COLLECT

or

DELECT DUPLICATE ENTRIES FROM TABLENAME>

Eg:

data : BEGIN OF ITAB1 OCCURS 0,

COL1 TYPE C,

COL2 TYPE I,

END OF ITAB1.

ITAB1-COL1 = ‘A’. ITAB1-COL2 = ‘1’.

COLLECT TAB1.

ITAB1-COL1 = ‘B’. ITAB1-COL2 = ‘2’.

COLLECT TAB1.

ITAB1-COL1 = ‘A’. ITAB1-COL2 = ‘1’.

COLLECT TAB1.

LOOP AT ITAB1.

write:/ ITAB1-COL1, ITAB1-COl2.

endloop.

This Produces the output as follows:

A 2

B 2

regards,

sreelakshmi.

Read only

Former Member
0 Likes
27,788

hi,

DELETE ADJACENT DUPLICATES FROM <itab>

[COMPARING <f1> .. <fn>|ALL FIEL <fn>}|ALL FIELDS}].

The system deletes all adjacent entries with the same key field contents apart from the first entry. You

can prevent the system from only comparing the key field using the COMPARING addition. If you sort

the table by the required fields beforehand, you can be sure that only unique entries will remain in the

table after the DELETE ADJACENT DUPLICATES statement.

Hope this helps, Do reward.

Read only

0 Likes
27,787

Hi,

Just sort the table with required key fields.

And write the query to delete adjacent duplicates.

No need to do comparing and all, It will take care. Here sort is important.

Regards,

Ramesh

Read only

Former Member
0 Likes
27,787

HI,

I HAVE ALSO GOT SAME PROBLEM,FEW DAYS BEFORE.

I HAVE SOLVED IT.

JUST WRITE

Delete adjacent duplicates from itab comparing pernr orgeh name .

OR

Delete adjacent duplicates from itab comparing pernr .

THANKS

Read only

Former Member
0 Likes
27,787

HI,

I HAVE ALSO GOT SAME PROBLEM,FEW DAYS BEFORE.

I HAVE SOLVED IT.

JUST WRITE

Delete adjacent duplicates from itab comparing pernr orgeh name .

OR

Delete adjacent duplicates from itab comparing pernr .

THANKS

Read only

Former Member
0 Likes
27,787

Hi,

Very Simple.....

Delete Adjacent Duplicates from itab comparing all fields.

(or)

According to your Internal table table

Delete Adjacent Duplicates from itab comparing pernr orgeh name i.d.

Regards,

V.Balaji

Read only

Former Member
27,787

hai Ricardo

Deleting Adjacent Duplicate Entries

To delete adjacent duplicate entries use the following statement:

DELETE ADJACENT DUPLICATE ENTRIES FROM <itab>

[COMPARING <f1> <f 2> ...

|ALL FIELDS].

The system deletes all adjacent duplicate entries from the internal table <itab>. Entries are duplicate if they fulfill one of the following compare criteria:

Without the COMPARING addition, the contents of the key fields of the table must be identical in both lines.

If you use the addition COMPARING <f1> <f 2> ... the contents of the specified fields <f 1 > <f 2 > ... must be identical in both lines. You can also specify a field <f i > dynamically as the contents of a field <n i > in the form (<n i >). If <n i > is empty when the statement is executed, it is ignored. You can restrict the search to partial fields by specifying offset and length.

If you use the addition COMPARING ALL FIELDS the contents of all fields of both lines must be identical.

You can use this statement to delete all duplicate entries from an internal table if the table is sorted by the specified compare criterion.

If at least one line is deleted, the system sets SY-SUBRC to 0, otherwise to 4.

Examples

DATA: BEGIN OF LINE,

COL1 TYPE I,

COL2 TYPE I,

END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.

DO 4 TIMES.

LINE-COL1 = SY-INDEX.

LINE-COL2 = SY-INDEX ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

LINE-COL1 = 1.

DELETE TABLE ITAB: FROM LINE,

WITH TABLE KEY COL1 = 3.

LOOP AT ITAB INTO LINE.

WRITE: / LINE-COL1, LINE-COL2.

ENDLOOP.

The output is:

2 4

4 16

The program fills a hashed table with a list of square numbers. The DELETE statement delete the lines from the table where the key field COL1 has the contents 1 or 3.

DATA: BEGIN OF LINE,

COL1 TYPE I,

COL2 TYPE I,

END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.

DO 4 TIMES.

LINE-COL1 = SY-INDEX.

LINE-COL2 = SY-INDEX ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

DELETE ITAB WHERE ( COL2 > 1 ) AND ( COL1 < 4 ).

LOOP AT ITAB INTO LINE.

WRITE: / LINE-COL1, LINE-COL2.

ENDLOOP.

The output is:

1 1

4 16

The program fills a hashed table with a list of square numbers. The DELETE statement deletes the lines of the table where the content of field COL2 is greater than 1 and the content of field COL1 is less than 4.