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

Diference calculation code

Former Member
0 Likes
619

itab1

material s720amount

m001 1000

m002 1200

m003 1300

m001 100

m004 0900

m003 130

m005 1800

m001 10

itab2

material s780amount

m001 0540

m003 1400

m001 200

m004 0300

m007 1200

i need output like this

Material s720amount s780amount difference

m001 (100010010)=1110 (540+200) =740 1110-740 = 370

m002 1200 0 1200

m003 1300+130 =13130 1400 difference --

5 REPLIES 5
Read only

Former Member
0 Likes
592

HI,

I assume that ur two Itabs has values..

Sort Itab1 ascending by Matnr.

sort Itab2 ascending by matnr.

declare itab4 and itab5 as same structures as itab1 and itab2 respectively.

loop at itab1.

itab4-matnr = itab1-matnr.

itab4-amount1 = itab1-amount1.

collect itab4.

endloop.

loop at itab2.

itab5-matnr = itab2-matnr.

itab5-amount2 = itab2-amount2.

collect itab5.

endloop.

Now itab4 and itab 5 has sums of amount for a same material.

loop at itab4.

itab3-matnr = itab4-matnr.

itab3-amount1 = itab4-amount1.

read table itab5 with key matnr = itab4-matnr.

if sy-subrc = 0.

itab3-amount2 = itab5-amount2.

endif.

append itab3.

endloop.

loop at itab3.

diff = itab3-amount1 - itab3-amount2.

modify itab3 transporting diff.

endloop.

loop at itab3.

Write: / itab3-matnr, itab3-amount1, itab3-amount2, itab3-diff.

endloop.

Regards

SAB

Read only

Former Member
0 Likes
592

hi,

sort the two tables based on material. then use COLLECT statement.

it will give the total of the amount for each material. then loop the first internal table and then read the 2nd internal table and find the difference based on material.

regards,

madhu

Read only

Former Member
0 Likes
592

Hi rejendran,

Get both the internal tables in one single internal table.

(by appending)

Then sort your both internal tables by material.

put the code as follows:

data: amount type i.

data: amount type itab-amount.

loop at the (large single internal table).

amount = itab-amount.

at new material.

amount1 = amount1 + amount.

end at.

at end of material.

amonut1 = 0.

end at.

endloop.

Read only

Former Member
0 Likes
592

Sorry Rajendran,

I misunderstood your requirement.

I will send you the code shortly.

Read only

RaymondGiuseppi
Active Contributor
0 Likes
592

Create itab3 material S720amount s780amount

Then

loop at itab1.

clear itab3.

move-corresponding itab1 to itab3.

collect itab3.

endloop.

loop at itab2.

clear itab3.

move-corresponding itab2 to itab3.

collect itab3.

endloop.

then

sort itab3 by matnr.

loop at itab3

write: / matnr, S720amount, s780amount, S720amount-s780amount.

endloop.

Regards