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: 

Deleting entries from an itab

Former Member
0 Kudos
121

Hi Guys,

Have a situation where I have to delete duplicates.OBJECTCLAS,objectid and changenr is the key.

when there are 2 entries as described in the testdata below ,where OBJECTCLAS,objectid and changenr is the same and if they have 2 or more tabnames(stpo,stas etc),then I want to retain the row with tabname STPO and delete other entries.

OBJECTCLAS	OBJECTID	CHANGENR	CHNGIND	VALUE_NEW	VALUE_OLD	TABNAME
STUE		020M00034022	26967781	U	2.000		1.000		STPO

STUE		020M00034022	26967793	I					STAS
STUE		020M00034022	26967793	I					STPO

STUE		020M00034022	26967794	I					mast
STUE		020M00034022	26967794	I					STPO

STUE		020M00034022	26967795	I					mast

The output from the testdata above after deletion should look as follows:

STUE		020M00034022	26967781	U	2.000		1.000		STPO
STUE		020M00034022	26967793	I					STPO
STUE		020M00034022	26967794	I					STPO
STUE		020M00034022	26967795	I					mast

Iam a bit confused about how to proceed.Any tip is welcome...

Thanks

Koh

10 REPLIES 10

gerd_rother
Active Participant
0 Kudos
79

Hi,

Use SORT to sort your table by those fields and DELETE ADJACENT DUPLICATES ... COMPARING to delete the superfluous entries.

See the documentation for these commands.

Regards, Gerd Rother

0 Kudos
79

But how will I make sure that by using delete adjacent duplicates in the following case that row with STAS is getting deleted and not the row with STPO.

STUE		020M00034022	26967793	I					STAS
STUE		020M00034022	26967793	I					STPO

0 Kudos
79

Hi,

If there are only those two values STAS and STPO possible you could sort the table with TABNAME DESCENDING which will make the STPO entry come first. Since DELETE ADJACENT DUPLICATES leaves the first entry of equal entries in the table the STPO entry will be kept (see documentation).

Regards, Gerd Rother

0 Kudos
79

As you can see from my first post(The testdata) ihave more than 2 entries.(Have read the documentation)

In my eyes I could

sort by OBJECTCLAS,objectid and changenr.

and then I have to some how make sure that stpo is the first entry from the duplicates. Thats what Iam trying to find out?

0 Kudos
79

Check this code snippet:

DATA:
      itab TYPE SORTED TABLE OF cdpos
      WITH NON-UNIQUE KEY objectclas objectid changenr,

      wa TYPE cdpos,

      itemp TYPE SORTED TABLE OF cdpos
      WITH NON-UNIQUE KEY objectclas objectid changenr,

      wa_temp TYPE cdpos,

      lv_index TYPE i VALUE '1',
      lv_counter TYPE i,

      it_final TYPE SORTED TABLE OF cdpos
      WITH NON-UNIQUE KEY objectclas objectid changenr.

* For Test only i select with OBJECTCLAS = `STUE`
SELECT * FROM cdpos INTO TABLE itab UP TO 1000 ROWS
  WHERE objectclas = `STUE`.

CHECK sy-subrc = 0.

itemp = itab.

* ITEMP contains only unique records
DELETE ADJACENT DUPLICATES FROM itemp
COMPARING objectclas objectid changenr.

LOOP AT itemp INTO wa_temp.
  LOOP AT itab INTO wa FROM lv_index.
    IF wa_temp-objectclas EQ wa-objectclas AND
       wa_temp-objectid   EQ wa-objectid AND
       wa_temp-changenr   EQ wa-changenr.

      lv_counter = lv_counter + 1.

*     For mutliple records keep the STPO record only
      CHECK lv_counter > 1 AND wa-tabname = 'STPO'.
      APPEND wa TO it_final.
    ELSE.
      IF lv_counter = 1."For Single records append to final table
        APPEND wa_temp TO it_final.
      ENDIF.
*   If the key fields change record the index & EXIT the loop
      lv_index = sy-tabix.
      EXIT.
    ENDIF.
  ENDLOOP.
ENDLOOP.

DELETE ADJACENT DUPLICATES FROM it_final
COMPARING objectclas objectid changenr.

@Keshav: What about records with TABNAME not equal to 'STPO' ? Then will not be considered, correct ?

Edited by: Suhas Saha on Jun 3, 2010 1:53 PM

0 Kudos
79

Keshav,

The last row with tabname- mast in my resultant test data is not coming in it2. ie:

STUE		020M00034022	26967795	I					mast

0 Kudos
79

Hm,

Still I do not see the problem if you do it this way:


sort it1 by OBJECTCLAS ascending 
                 OBJECTID ascending
                 CHANGENR ascending
                 TABNAME descending.
delete adjacent duplicates from it1 comparing OBJECTCLAS OBJECTID CHANGENR.

This should only leave those entries in the table you wanted, why should it not?

Regards, Gerd Rother

matt
Active Contributor
0 Kudos
79

This is a fairly basic programming problem. Not really suitable for these forums. Thread locked.

former_member536879
Active Contributor
0 Kudos
79

Hi,

Please See the SCN Rules Before Posting.

With Regards,

Sumodh.P

kesavadas_thekkillath
Active Contributor
0 Kudos
79

Can you check this


loop at it1 into wa.
if wa-tabname = 'STPO'
read table it2 with key objectclas = wa-objectclas 
			OBJECTID = wa-OBJECTID
			CHANGENR - wa-CHANGENR
			TABNAME = wa-tabname transporting no fields.
if sy-subrc ne 0.
append wa to it2.
endif.
endif.
endloop.

it2 will hold your final values