2013 Jun 05 11:22 AM
Hi folks,
I have a requirement to delete adjacent duplicates from a table comparing a field only if it has value.
DELETE ADJACENT DUPLICATES FROM itab COMPARING matnr.
to be precise, i don't want to delete the data line from itab if 2 records have matnr as SPACE.
could someone give me the best possible solution?
Thanks,
Zid.
2013 Jun 05 11:51 AM
HI Ziden,
I don't know why u need 2 Spaces but for this requirement try the below code.
itab1[] = itab2[].
sort itab1 by matnr.
delete adjacent duplicates from itab1 comparing matnr.
delete itab1 where matnr is initial.
delete itab2 where matnr is not initial.
append lines of itab2 to itab1.
itab1 is your final internal table.
Hi folks,
I have a requirement to delete adjacent duplicates from a table comparing a field only if it has value.
DELETE ADJACENT DUPLICATES FROM itab COMPARING matnr.
to be precise, i don't want to delete the data line from itab if 2 records have matnr as SPACE.
could someone give me the best possible solution?
Thanks,
Zid.
2013 Jun 05 11:51 AM
HI Ziden,
I don't know why u need 2 Spaces but for this requirement try the below code.
itab1[] = itab2[].
sort itab1 by matnr.
delete adjacent duplicates from itab1 comparing matnr.
delete itab1 where matnr is initial.
delete itab2 where matnr is not initial.
append lines of itab2 to itab1.
itab1 is your final internal table.
2013 Jun 05 12:00 PM
Hi!
You are correct...
Only change would be,
"delete itab1 where matnr is initial." should come after "sort itab1 by matnr."
So your code is:
itab1[] = itab2[].
sort itab1 by matnr.
delete itab1 where matnr is initial.
delete adjacent duplicates from itab1 comparing matnr.
delete itab2 where matnr is not initial.
append lines of itab2 to itab1.
Regards,
KS
2013 Jun 05 12:03 PM
Try this logic.
SORT itab BY fld1.
LOOP AT itab INTO wa WHERE fld1 IS INITIAL.
APPEND wa TO itab1.
CLEAR wa.
ENDLOOP.
DELETE ADJACENT DUPLICATES FROM itab COMPARING fld1.
append lines of itab1 to itab.
SORT itab BY fld1.
2013 Jun 05 12:10 PM
you mean :
LOOP AT itab INTO wa WHERE fld1 IS NOT INITIAL. "Adding NOT
APPEND wa TO itab1.
CLEAR wa.
ENDLOOP.
DELETE ADJACENT DUPLICATES FROM itab1 COMPARING fld1. "changing from itab to itab1
delete itab where matnr is not initial.
append lines of itab1 to itab.
Regards,
KS
2013 Jun 05 1:01 PM
i just gave matnr as example.
My actual req is with a z-text field.
How i wish SAP had given a provision to have it in the syntax itself.
like...delete adujcent duplicates from itab3 comparing matnr IGNORING spaces!?!
seems this is optimum in the absense of above... i strictly wanted to avoid loops.
Thanks.
2013 Jun 05 12:17 PM
hi try this
1. define itab2 , itab 3 same as itab1.
2. loop at itab1 into wa1
if wa1_matnr = ' '.
append wa1 to itab2
else.
append wa1 to itab3.
endif.
3. refresh itab1
4 delete adujcent duplicates from itab3 comparing matnr
5 append itab3 to itab1
append itab2 to itab1
6. sort itab1 by matnr.
hope it will work
suresh