Suppressing duplicate entries in classical and ALV report in SAP ABAP


To delete adjacent duplicate entries in an internal table, you can use the below command −

DELETE ADJACENT DUPLICATE ENTRIES FROM <itab>
                     [COMPARING <f1> <f 2> ...
                         |ALL FIELDS].

Also, consider the below points −

  • 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.

Example

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.

Output

The output is −

2        4
4       16

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

 Example

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.

Output

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.

Updated on: 12-Mar-2020

937 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements