2010 Jun 03 8:19 AM
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
2010 Jun 03 8:33 AM
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
2010 Jun 03 8:39 AM
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
2010 Jun 03 8:48 AM
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
2010 Jun 03 8:58 AM
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?
2010 Jun 03 9:22 AM
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
2010 Jun 03 9:30 AM
Keshav,
The last row with tabname- mast in my resultant test data is not coming in it2. ie:
STUE 020M00034022 26967795 I mast
2010 Jun 03 9:39 AM
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
2010 Jun 03 10:09 AM
This is a fairly basic programming problem. Not really suitable for these forums. Thread locked.
2010 Jun 03 8:56 AM
Hi,
Please See the SCN Rules Before Posting.
With Regards,
Sumodh.P
2010 Jun 03 9:01 AM
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