Thursday, July 19, 2012

Delete Duplicate Records

REPORT Z_TEST_REPORT.
data:

BEGIN OF itab OCCURS 0,

kunnr type kna1-kunnr,
name1 type kna1-name1,

END OF itab.

START-OF-SELECTION.

"we want to get data from the customer table where the keys
"are found in the sales order table (VBAK)

select kna1~kunnr kna1~name1 into CORRESPONDING FIELDS OF TABLE
itab from kna1 inner join vbak on
kna1~kunnr = vbak~kunnr.

sort itab by kunnr.

"There should be multiple customers data selected,
"Now if you want to show only 1 records per customer
"and remove all the duplicate rows, you can use the
"Delete Adjacent duplicates statement.

"Using Adjacent Duplicates to delete multiple rows
delete ADJACENT DUPLICATES FROM itab COMPARING kunnr.

LOOP AT itab.
WRITE :/ itab-kunnr, itab-name1.
ENDLOOP.

Here’s a snapshot from itab at debugger screen, as you see there are multiple customers id in the internal table.

kunnr

Here’s the result, now you don’t see any duplicates records. The comparing block at the Adjacent duplicates syntax can consists of more than one fields, for example comparing kunnr name1.

kunnr2

No comments:

Post a Comment