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

internal table logic needed

Former Member
0 Likes
677

i have an internal table with fields like material , plant , amount etc.

we have plants like mts1,mts1,mts3 etc. and each plant have number of entries like 10 materials preent in mts1 and 20 materials present in mts2 etc and the amounts are also present. now i want to add the amounts for the plant wise. for mts1 there are 10 materials , so i need to add that 10 materials , so tell me how to do it .

is it by at last or by any other method.

or at the time of selecting from the database itself can we do that?

5 REPLIES 5
Read only

Mohamed_Mukhtar
Active Contributor
0 Likes
638

>

> i have an internal table with fields like material , plant , amount etc.

> we have plants like mts1,mts1,mts3 etc. and each plant have number of entries like 10 materials preent in mts1 and 20 materials present in mts2 etc and the amounts are also present. now i want to add the amounts for the plant wise. for mts1 there are 10 materials , so i need to add that 10 materials , so tell me how to do it .

> is it by at last or by any other method.

> or at the time of selecting from the database itself can we do that?

Loop at <internal table>

at end of plant.
sum.
endat.
endloop,

Thanks & Regards

Read only

lijisusan_mathews
Active Contributor
0 Likes
638

Hi,

If you are using an ALV to display the out(FM : REUSE_ALV_LIST_DISPLAY), all you have to do is to sort the internal table based on plant, and give subtotals field = X in layout and in field catalog give do_sum = X for the fields you want to sum up. (i hope you r familiar with THIS FM). The totals and subtotals will be done automatically.i think this is the easier method

if you are using standard list output using write statements, then you will have to code for this.. you can use 2 approaches mainly

1) use at end to sum up - but in this case, make sure you have all the records summed up. I have observed while using At end that the last record sometimes does not get added up.in this case , you will have to take measures to add that too.

2) use an alternate code

sort table by plant.

read table table into wa_temp index 1.

loop at table into wa.

if wa-plant ne wa_temp-plant.

wa_temp = wa.

"" here write the code for appending or writing or anything else as required.

""sum = 0.

endif.

sum = sum + wa-sum.

endloop.

"read the last line of the table and take the necessary steps with it if t has not been added up yet.

Read only

Former Member
0 Likes
638

Hi

First you select the field into one internal table itab.

Sort them based on plant.

if you are using at end of plant ,you have to make sure that plant is first field in the internal table else

suppose material is the first field in the internal table and plant is second their are 10 diffrent material per plant then your at end of statement will be executed 10 times.

If you want to avoid at end of then you can use ALV .

Regards

Neha

Read only

Former Member
0 Likes
638

hi

Maybe you can look the help about these sentences:

at first, at last, at end, at new

These sentences can help you for solve your problems

Regards

Gregory

Read only

Former Member
0 Likes
638

You can use a numbe of different ways , however I like to maintain sums in separate tables based on the key fields.

1) What you can do is collect this internal table into another internal table.

or

2) As other have mentioned...use the SUM command with the "at end of" or on change of command, you should however have your table sorted accordingly..