Application Development and Automation 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: 
Read only

Efficiently deleting lines from internal table based on column comparison

Former Member
0 Kudos
459

Hi all,

let's assume, I have the following internal table MY_TAB:


COL_1 | COL_2 | COL_3
A       1       1
B       2       2
C       3       3
D       3       4
E       3       5

I'd like to delete all rows, where the values in COL_2 and COL_3 are equal. That is, in the above table, the rows for A, B and C.

What's the most performant / fastest and efficient way to do this?

Thanks for any hints in advance!

Best regards,

Philipp

1 ACCEPTED SOLUTION
Read only

Former Member
0 Kudos
428

if the table has only short fields as above then ASSIGNING has no advantage.

Use it with broader tables.

But overall, the ASSIGNING is an additional contribution.

The wrong command for the DELETE gives a completely different scaling !!!

8 REPLIES 8
Read only

BH2408
Active Contributor
0 Kudos
428

Hi,

Copy the internal table into Temp table say My_tab_tmp.

append line of my_tab to my_tab_tmp.

sort my_tab_tmp by col2 col3.

delete duplicate entries from my_tab_tmp by comparing col1 col2.

loop at my_tab_tmp into wa_my_tab_tmp.

read tbale my_tab into wa_tab with key col1 = wa_my_tab_tmp-col1 binary search.

if (wa_my_tab_tmp-col2 = wa_tab-col2) and (wa_my_tab_tmp-col3 = wa_tab-col3).

delete my_tab from wa_tab where col1 = wa_my_tab_tmp-col1.

endif.

endloop.

Regards,

Bharani

Read only

Former Member
0 Kudos
428

I do not understand other proposal.

The are two solutions possible


LOOP AT itab INTO wa.

   IF ( wa-col2 = wa-col3).
     DELETE itab INDEX sy-tabix.
   ENDIF.

ENDLOOP.

If memory is not an issue and if many lines are to be deleted, then the APPEND in a new table is better.#

I would use that one in all cases where the conditions are complicated.


LOOP AT itab INTO wa.
   IF ( wa-col2 <> wa-col3).
     APPEND wa TO itab2.
   ENDIF.
ENDLOOP.
refresh itab.
itab[] = itab2[].

Siegfried

Read only

Former Member
0 Kudos
428

Hi,

Try:

  
  field-symbols: <f> like line of itab.

  loop at itab assigning <f>.
     if <f>-col1 = <f>-col2.
        delete itab index sy-tabix.
     endif.
  endloop.

Regards,

Edited by: Diego Alvarez on Feb 3, 2010 5:45 PM

Read only

0 Kudos
428

Hi

Assigning field symbols or looping the internal table by comnparison would be the best solution.

Siva

Read only

Former Member
0 Kudos
429

if the table has only short fields as above then ASSIGNING has no advantage.

Use it with broader tables.

But overall, the ASSIGNING is an additional contribution.

The wrong command for the DELETE gives a completely different scaling !!!

Read only

0 Kudos
428

Hi Siegfred,

The aim of using "assigning" is to avoid copying each line of the table to the header structure and only change the memory address that is pointed by the field-symbol. In small tables, will not be noticeable difference, but when you have many thousands of lines you will notice it is faster, you will avoid the step of copying data from one place to another... I think so, though I could be wrong

Regards,

Read only

0 Kudos
428

Hi all,

I think you gave me some useful hints for my problem! Thanks!

I might run some performance test and see what solution is the fastest...

Regards,

Philipp

Read only

Former Member
0 Kudos
428

Hi,

DELETE my_tab WHERE col2 EQ col3.

sorry, its my fault

Marcel

Edited by: MarcelKucharek on Feb 4, 2010 10:10 AM

Edited by: MarcelKucharek on Feb 4, 2010 10:11 AM