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

subtract two internal tables

Former Member
0 Likes
2,482

Hi All!

I have two int tables as below with the same columns and field names and values below..

Item quan

mfg03 50

mfg04 100

mfg05 20.

second IT have the same item numbers but different values.Now my requirement is to <u><b>SUBTRACT</b></u> the quantities into 3rd IT item wise.Please advise.

Regards

Pavan

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,233

Hi Kumar,

Take third interna table and do as follows.

<b>1)If you sure that you dont have more than one entry for each material in ITAB2 then do as follows.</b>

DATA ITAB3 TYPE ITAB OCCURS 0 WITH HEAEERLINE.

loop at itab1.

read table itab2 with key item = itab1-item.

wa_itab3-quan = itab1-quan - itab2-quan.

wa_itab3-item = itab1-item.

append wa_itab3 to itab3

endloop.

<b>2)If you more than one entry for each material in ITAB2 then do as follows.</b>

loop at itab1.

loop at itab2 where item = itab1-item.

wa_itab3-quan = itab1-quan - itab2-quan.

wa_itab3-item = itab1-item.

endloop.

append wa_itab3 to itab3

endloop.

Thanks,

Vinay

6 REPLIES 6
Read only

Former Member
0 Likes
1,233

Loop at itab1.

Read table itab2 with key item = itab1-item.

If sy-subrc eq 0.

itab1-qty = itab1-qty - itab2-qty.

Modify itab1.

Endif.

Endloop.

Read only

Former Member
0 Likes
1,233

loop at itab1.

read table itab2 with key item = itab-item.

wa_itab3-quan = itab1-quan - itab2-quan.

wa_itab3-item = itab1-item.

append wa_itab3 to itab3

endloop.

Regards,

Ravi

Read only

Former Member
0 Likes
1,233

loop at itab1 into wa_itab1

read table itab2 into wa_itab2 with key item = wa_itabl-item.

wa_itab3-item = wa_itab1-item.

wa_itab3-quan = wa_itab1-quan - wa_itab2-quan.

append wa_itab3 to itab3.

clear wa_itab3.

endloop.

Read only

Former Member
0 Likes
1,233

Hi,

sort itab2 by item.

loop at itab1 into wa_itab1

read table itab2

into wa_itab2

with key item = wa_itab1-item

binary search.

wa_itab3-item = wa_itab1-item.

wa_itab3-quan = wa_itab1-quan - wa_itab2-quan.

append wa_itab3 to itab3.

clear wa_itab3.

endloop.

Read only

Former Member
0 Likes
1,234

Hi Kumar,

Take third interna table and do as follows.

<b>1)If you sure that you dont have more than one entry for each material in ITAB2 then do as follows.</b>

DATA ITAB3 TYPE ITAB OCCURS 0 WITH HEAEERLINE.

loop at itab1.

read table itab2 with key item = itab1-item.

wa_itab3-quan = itab1-quan - itab2-quan.

wa_itab3-item = itab1-item.

append wa_itab3 to itab3

endloop.

<b>2)If you more than one entry for each material in ITAB2 then do as follows.</b>

loop at itab1.

loop at itab2 where item = itab1-item.

wa_itab3-quan = itab1-quan - itab2-quan.

wa_itab3-item = itab1-item.

endloop.

append wa_itab3 to itab3

endloop.

Thanks,

Vinay

Read only

Former Member
0 Likes
1,233

Hi,

You can subtract the field which you don't want like this here itab2 is that internal table where you move data form itab1 and sort itab2 by key and if there is any duplicate entry is there use delete adjacent.

LOOP AT itab1.

MOVE-CORRESPONDING itab1 TO itab2.

APPEND itab2.

Endloop .

SORT itab2 BY key.

DELETE ADJACENT DUPLICATES FROM itab2.