Skip to Content
avatar image
Former Member

Delete duplicate from internal table

HI Abapers,

I have a query on how to remove the duplicates from an internal table

My internal table data is as follows :

Cno Catg1 Catg2

01 0 1000

01 2000 0

I want to get only one record as

01 2000 1000

How to get the result.

I tried sorted by cno and used delete duplicates but it was not helpful.

Is there any other alternative to get this done

Please help me.

Regards,

Priya

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

8 Answers

  • avatar image
    Former Member
    Oct 09, 2007 at 09:16 AM

    check it out with delete adjacent duplicate records

    Deleting Adjacent Duplicate Entries

    To delete adjacent duplicate entries use the following statement:

    DELETE ADJACENT DUPLICATE ENTRIES FROM <itab>

    [COMPARING <f1> <f 2> ...

    |ALL FIELDS].

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 09, 2007 at 08:43 AM

    Hi Priya,

    You Can use Collect Statement.

    COLLECT is used to Summarize the Data in internal table while adding the rows.

    Collect <wa> into <itab>.

    This statement compares the Non-numeric(Type C,N,D,T,X,String) fields of the work area with the Existing rows in the internal table. that means all the Non-numeric fields will act as key (For Eg Matno, Plant)

    If a row is found with the same key:

    It will add the Numeric fields instead of creating a new row.

    If a row is not found with the same key:

    It will create a new row like Append.

    DATA : BEGIN OF ITAB1 OCCURS 0,

    MATNR TYPE MARD-MATNR,

    WERKS TYPE MARD-WERKS,

    LABST TYPE MARD-LABST,

    END OF ITAB.

    DATA :WA LIKE ITAB1.

    DATA: ITAB2 LIKE ITAB1 OCCURS 0.

    SELECT MATNR WERKS LABST FROM MARD INTO TABLE ITAB1.

    LOOP AT ITAB1 INTO WA.

    COLLECT WA INTO ITAB2.

    ENDLOOP.

    Check the contents of both ITAB1 AND ITAB2.

    Thanks.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 09, 2007 at 08:44 AM

    sort itab by cno catg1 descending.

    delete adjacent duplicates from itab comparing cno.

    Regards

    Vasu

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 09, 2007 at 08:52 AM

    Hi,

    try to sort the internal table based on the three field.

    Like this.

    Soirt it_tab1 by cno catg1 catg2.

    DELETE ADJACENT DUPLICATES FROM lt_tab1 comparing cno catg1 catg2.

    or try this logic.

    read table it_tab1 into ls_tab1_temp index 1.

    l_count = 0.

    loop at it_tab1 into ls_tab1.

    if ls_tab1-cno eq ls_temp_temp-cno. <<add the conditions>>

    l_count = l_count + 1.

    endif.

    if l_count GT 1.

    delete it_tab1 from ls_tab1_temp index sy-tabix.

    clear l_count.

    endif.

    ls_tab1_temp = ls_tab1.

    endloop.

    Regards,

    Niyaz

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 09, 2007 at 08:57 AM

    hi,

    try like this

    Loop at itab into watab.

    read table itab into watab1 where cno = watab-cno.

    if sy-subrc eq 0.

    move : watab1-cno to watab2-cno.

    if watab-catg1 LT watab1-catg1.

    move : watab1-catg1 to watab2-catg1.

    append itab1 from watab2.

    else.

    move : watab-catg1 to watab2-catg1.

    append itab1 from watab2.

    endif.

    if watab-catg2 LT watab1-catg2.

    move : watab1-catg2 to watab2-catg2.

    modify itab1 from watab2.

    else.

    move : watab-catg2 to watab2-catg2.

    modify itab1 from watab2.

    endif.

    endif.

    endloop.

    reward if helpful.

    Regards,

    nagaraj

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 09, 2007 at 08:58 AM

    Hi,

    Go through example

    DATA: BEGIN OF connection,

    cityfrom TYPE spfli-cityfrom,

    cityto TYPE spfli-cityto,

    distid TYPE spfli-distid,

    distance TYPE spfli-distance,

    END OF connection.

    DATA connection_tab LIKE SORTED TABLE OF connection

    WITH NON-UNIQUE KEY cityfrom cityto

    distid distance.

    SELECT cityfrom cityto distid distance

    FROM spfli

    INTO TABLE connection_tab.

    DELETE ADJACENT DUPLICATES FROM connection_tab.

    Reward if useful.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 09, 2007 at 09:28 AM

    no u cant use delete adjacent duplicates and get the o/p u needed in this case.

    one solution is collect statement, while inserting record itself, if u use collect u can get the o/p.

    or else u have to write some logic

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 10, 2007 at 04:36 AM

    Thanks all of u , By using the collect statement i am able to get the desired result

    Add comment
    10|10000 characters needed characters exceeded