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

calculation in loop stmt

Former Member
0 Likes
1,811

Hi all,

i have a requirement to calculate total quantity for each and every material,

fetched data from MSEG table for multiple material's and need to calculate the total of every material.

am little bit confusing while summing the quantity(MSEG-MENGE) for each individual material.

my internal table contains materiel number, quantity and Base UOM.

need sample code...

Regards

Suresh

8 REPLIES 8
Read only

Former Member
0 Likes
1,576

HI,

Use COLLECT statement.

Suppose your internal table is ITAB and it contains the following fields.

MATNR UOM QTY

Create one more table which is same as ITAB.

Do as follows:-

SORT ITAB BY MATNR UOM.

LOOP AT ITAB INTO WA.

COLLECT WA INTO ITAB2.

ENDLOOP.

Regards.

Ankur Parab

Read only

0 Likes
1,576

Hi,

Declare one more internal table itba2.

Sort table itab1 by Matnr and UoM.

Then loop table itab1.

Inside loop use Collect stmt, which will append record of itab1 to itab2 in such a way that u will get sum of each Quantity.

Eg

Itab1

Material Quantity Uom

1000 10 KG

1000 10 KG

1001 10 Kg

As Per Collect Stmt

Itab2

Material Quantity Uom

1000 20 KG

1001 10 KG

Read only

Former Member
0 Likes
1,576

suresh, i am not able to understand the exact requirement. can you please explain what exactly you need?

you can sum them in each line and use at end-of material to update it to some other table.

will this solve your problem?

Read only

0 Likes
1,576

there are multiple entries for same material, and need to calculate total for each material,

my internal table contains material number and quantity

Read only

0 Likes
1,576

Hi Suresh,

Try the following code.

Get all the distinct material numbers in to a temporary internal table,


data: begin of it_temp occurs 0,
        matnr like mseg-matnr,
       menge like mseg-menge,
 end of it_temp.


loop at it_mseg.
at new of matnr.
move-corresponding it_mseg to it_temp.
append it_temp.
endat.
endloop.

sort it_temp by matnr.

After this calculate the quantity as per below


loop at it_temp.
loop at it_mseg where matnr = it_temp-matnr.
wa_menge = it_mseg-menge.
it_temp-menge = it_temp-menge + wa_menge.
endloop.
modify it_temp.
clear it_temp-menge.
endloop.

Regards,

Vik

Edited by: vikred on Jul 22, 2009 9:59 AM

Read only

Former Member
0 Likes
1,576

SORT the internal stable I_MSEG by MATNR.

Use AT NEW MATNR statement within the loop.

and initilize the total qty within that.

After calculate sum of qty for material as

t_qty = t_qty + i_mseg-menge.

and display the report using AT END OF MATNR statement.

Regds,

ANil

Read only

0 Likes
1,576
sort gt_mseg by matnr.

loop at gt_mseg into gs_mseg.
     gv_qty = gv_qty + gs_mseg-menge.
 at end og matnr.
     gs_final-qty = gv_qty.
     appen gs_ginal to gt_final.
     clear: gv_qty.
 endat.

endloop.

do a similar code.

Read only

Former Member
0 Likes
1,576

solved