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

Calculate amount

Former Member
0 Likes
1,066

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?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,034

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.

7 REPLIES 7
Read only

Former Member
0 Likes
1,035

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.

Read only

Former Member
0 Likes
1,034

Hi Vamsee ,

As suggested in the previous post you can use the collect statement.

Regards

Arun

Read only

Former Member
0 Likes
1,034

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

Read only

varma_narayana
Active Contributor
0 Likes
1,034

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>

Read only

0 Likes
1,034

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?

Read only

0 Likes
1,034

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

Read only

Former Member
0 Likes
1,034

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