‎2007 Sep 07 4:59 AM
Hi,
I have an Internal table which looks like below
Customer Amount
0001 20.00
0001 30.00
0002 34.00
0002 40.02
I need to calculate the total amount per customer, which is Customer 0001 will have a total of 50 and 0002 has a total amount of 74.02.
Does anyone know how to code this , by not including tooo many loop statments?
‎2007 Sep 07 5:04 AM
Suppose the data provided by u is in the internal table itab1.
now code like this,
sort itab1 by customer.
loop at itab1.
move-corresponding itab1 to itab2.
collect itab2.
endloop.
here itab2 having the same structure as itab1 is having.
By this coding itab2 will have the data as u requires.
‎2007 Sep 07 5:04 AM
Suppose the data provided by u is in the internal table itab1.
now code like this,
sort itab1 by customer.
loop at itab1.
move-corresponding itab1 to itab2.
collect itab2.
endloop.
here itab2 having the same structure as itab1 is having.
By this coding itab2 will have the data as u requires.
‎2007 Sep 07 5:08 AM
Hi Vamsee ,
As suggested in the previous post you can use the collect statement.
Regards
Arun
‎2007 Sep 07 5:09 AM
Hello Vamsi,
You can use loop ,at end of command.
your internal table is ITAB.
take one more internal table like ITAB
That is ITAB1
data val type vbap-netwr.
loop at itab into wa_itab.
val = wa_itab-amount + val.
at end of kunnr.
itab1-kuunr = wa_itab-kunnr.
itab1-amount = val.
append itab1.
clear val.
endat.
clear wa_itab.
endloop.
see the results now.
Thanks
Seshu
‎2007 Sep 07 5:11 AM
Hi..
Sort itab by customer. "if it is not sorted already
Loop at Itab.
write:/ itab-customer, itab-amount.
AT END OF CUSTOMER.
SUM.
Write:/ 'Total Amount = ', itab-amount.
ENDAT.
Endloop.
<b>Reward if Helpful.</b>
‎2007 Sep 07 5:43 AM
Thanks guys for the fast responses.Did award the points.
one more question, i also have company code in my internal table, so how can i do the same based on the company code?
‎2007 Sep 07 5:53 AM
Hi Vamsee ,
What collect statement does is that is sum up all the numeric fields if all the non numeric fields are the same.
So if what you are looking for is a sum on company code and customer then the same statement must serve the purpose.
Regards
Arun
‎2007 Sep 07 5:31 AM
Hi Vamsee,
You can do like this also.
take another internal table of same structure it_second and a variable sum.
sum = 0.
loop at it_first into wa_first.
sum = sum + wa_first-amount.
at end of customer.
wa_second-customer = wa_first-customer.
wa_second-amount = sum.
append wa_second to it_second.
sum = 0.
endat.
endloop.
Regards,
Sheron