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

Adding values in same field inside loop

Former Member
0 Likes
7,475

Dear Experts,

suppose in my o/p i am displaying material number and its value:

:
material No:      Qty 
matnr                1000
matnr                2000
matnr                3000
matnr                4000
matnr                5000

Now I want my third field  ( SUM ) such that the value displayed should
get added to the next value displayed, till the loop ends for each particular material.

material No:     Qty           Sum
matnr               1000        1000     " (1000)
matnr               2000        3000     " (1000 + 2000)
matnr               3000        6000     " (1000 + 2000 + 3000)
matnr               4000      10000     " (1000 + 2000 + 3000 + 6000)
matnr               5000      15000     " (1000 + 2000 + 3000 + 6000 + 10000)

Actually my requirement is :

First I need to get material and Quantity from MSEG table

then I need to compare this material and quantity in MBEW table to get the total stock

particular material.

so my condition is like :

if i_mseg~sum GE mbew-lbkum.

delete i_mseg.

endif.

Please advice.

15 REPLIES 15
Read only

Former Member
0 Likes
3,592

Hi Karthik...

take "SUM" field into your internal table and On loop processing assign this to a field-symbol and sum up the quantities and put in Sum field.

Remember dont delete the entries inside the loop and mark it with a flag and delete outside loop for better performance .

Regards,

Kranti Yamparala.

Read only

0 Likes
3,592

hi

lv_sum = 0.

loop at itab into wa.

lv_sum= lv_sum + wa-qty.

wa_final_matnr = wa_matnr.

wa_final_sum = lv_sum.

append lt_final.

endloop.

if material is multiple then use break event statement

on change of matnr.

lv_sum = 0.

at new,at end

Read only

0 Likes
3,592

Dear Dharma ,

Sum is not getting inside loop , but the same value is getting moved to sum.

Please advice

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
3,592

loop at itab into wa.

skip 1.

write at 10 wa-matnr.

write at 20 wa-qty.

lv_sum = lv_sum + wa-qty.

write at 30 lv_sum.

at end of matnr.

clear lv_sum.

endat.

endloop.

Read only

0 Likes
3,592

data : begin of ty_data.

material_No type matnr,

qty type qty,

sum type sum,

end of ty_data.

Declare internal table and work area of this.

data total_sum = 0.

loop at it_data into wa_data.

lv_sum = lv_sum + current_sum.

wa_data-material_no = matnr.

wa_data-qty = qty.

wa_data-sum = lv_sum.

append wa_data to it_data.

endloop.

Read only

Former Member
0 Likes
3,592

//Now I want my third field ( SUM ) such that the value displayed should

get added to the next value displayed, till the loop ends for each particular material.

material No: Qty Sum

matnr 1000 1000 " (1000)

matnr 2000 3000 " (1000 + 2000)

matnr 3000 6000 " (1000 + 2000 + 3000)

matnr 4000 10000 " (1000 + 2000 + 3000 + 6000)

matnr 5000 15000 " (1000 + 2000 + 3000 + 6000 + 10000)

//

explain the ouput of third field clearly.. im not able to understand which is getting added where ?

Br,

Vijay.

Read only

0 Likes
3,592

Hi,

I am displaying material , its quantity and sum as mentioned below :

matnr1  1st qty         sum = 1st qty
matnr1  2nd qty        sum = 1st qty +  2nd qty.
matnr1  3rd qty         sum = 1st qty +  2nd qty + 3rd qty.
matnr1  4th qty         sum = 1st qty +  2nd qty + 3rd qty + 4th qty.

matnr2  1st qty         sum = 1st qty
matnr2  2nd qty        sum = 1st qty +  2nd qty.
matnr2  3rd qty         sum = 1st qty +  2nd qty + 3rd qty.
matnr2  4th qty         sum = 1st qty +  2nd qty + 3rd qty + 4th qty.

hope its clear now.

Edited by: Karthik R on Marjavascript:void(0); 1, 2010 4:31 PM

Read only

0 Likes
3,592

data:lv_sum(30).

lv_sum = 0.

example for the material 'PEN' you are calculating the sum you can use this coding

loop at itab into wa.

lv_sum = lv_sum + wa-qty.

wa_final_sum = lv_sum.

wa_final_matnr = wa_matnr.

append lt_final.

endloop.

For mutiple material you can use Keshav coding....

Debug the code.

Read only

0 Likes
3,592

So you did not try keshavs code .. the answer is already given to you ..

Execute the code for your understanding ..

 
data : begin of itab occurs 0,
       f1 type matnr,
       f2 type i ,
       f3 type i,
       end of itab.

itab-f1 = 'MAT1'.
itab-f2 = '1000'.
append itab.

itab-f1 = 'MAT1'.
itab-f2 = '2000'.
append itab.

itab-f1 = 'MAT1'.
itab-f2 = '3000'.
append itab.

itab-f1 = 'MAT1'.
itab-f2 = '4000'.
append itab.

itab-f1 = 'MAT1'.
itab-f2 = '5000'.
append itab.

itab-f1 = 'MAT2'.
itab-f2 = '2000'.
append itab.

itab-f1 = 'MAT2'.
itab-f2 = '7000'.
append itab.


data : lv_flag type c,
       lv_sum type i.
loop at itab.
  at new f1.
     lv_flag = 'X'.
  endat.

  if lv_flag = 'X'.
    lv_sum = lv_sum + itab-f2.
  endif.
  itab-f3 = lv_sum.
 write:/ itab-f1 , itab-f2, itab-f3.
  
 at end of f1.
  clear : lv_sum, lv_flag.
  endat.

endloop.

Br,

Vijay.

Read only

0 Likes
3,592

hi kartik,

if you want sum with each line of matnr and quantity then try like this.


types : begin of ty_matnr,
           matnr type matnr,
           qty    type  quantity,  " data element of your quantity field
          sum  type   n,  " numeric or integer field
         end of ty_matnr.
data : it_matnr type table of ty_matnr,
         wa_matnr typr ty_matnr.
lv_sum = 0.
loop at it_matnr into wa_matnr.
lv_sum = lv_sum +wa_matnr-qty.
wa_matnr-sum = lv_sum.
modify it_matrn from wa_matnr index sy-tabix modifying  sum.

at end of matnr.
lv_sum = 0.
endat.
endloop.

if you want sum of one particular matnr then try like this.


types : begin of ty_matnr,
           matnr type matnr,
           qty    type  quantity,  " data element of your quantity field
          sum  type   n,  " numeric or integer field
         end of ty_matnr.
data : it_matnr type table of ty_matnr,
         wa_matnr typr ty_matnr.
lv_sum = 0.
loop at it_matnr into wa_matnr.
lv_sum = lv_sum +wa_matnr-qty.


at end of matnr.
wa_matnr-sum = lv_sum.
modify it_matrn from wa_matnr index sy-tabix modifying  sum.
lv_sum = 0.
endat.
endloop.

i hope this will help you in solving your query.

Thanks

Tanmaya

Read only

former_member404244
Active Contributor
0 Likes
3,592

Hi,

You can very well use Collect statement.. It will sum up both the qty and sum fields for each material.

Regards,

Nagaraj

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
3,592

Did you try my code

Read only

Former Member
0 Likes
3,592

Hi, Karthik,

data total type caufv-gamng.
data : begin of itab,
matnr type matnr,
qty     type gamng,
sum   type gamng,
end of itab,
wa like line of itab.

sort itab by matnr.
clear total. " Zero initially

loop at itab into wa.
total = itab-wa + total. 
wa-sum  = total.
modify itab from wa index sy-tabix.
write : / wa-matnr,
             wa-qty,
             wa-sum.

at end of matnr.
clear total.
endat.
endloop.

Cheerz

Ram

Read only

Former Member
0 Likes
3,592

Hi Karthik ,

Pls try this.

REPORT ztest.

TYPES: BEGIN OF ty_mseg,

matnr TYPE mseg-matnr,

menge TYPE mseg-menge,

sum TYPE mseg-menge,

END OF ty_mseg.

TYPES : BEGIN OF ty_mbew,

matnr TYPE mseg-matnr,

lbkum TYPE mbew-lbkum,

END OF ty_mbew.

DATA : it_mseg TYPE TABLE OF ty_mseg,

wa_mseg TYPE ty_mseg,

it_mbew TYPE TABLE OF ty_mbew,

wa_mbew TYPE ty_mbew.

DATA : sum TYPE mseg-menge.

START-OF-SELECTION.

wa_mseg-matnr = '1111'.

wa_mseg-menge = '1000'.

APPEND wa_mseg TO it_mseg.

wa_mseg-matnr = '1112'.

wa_mseg-menge = '1000'.

APPEND wa_mseg TO it_mseg.

wa_mseg-matnr = '1113'.

wa_mseg-menge = '1000'.

APPEND wa_mseg TO it_mseg.

SELECT matnr lbkum

FROM mbew INTO TABLE it_mbew

FOR ALL ENTRIES IN it_mseg

WHERE matnr = it_mseg-matnr.

LOOP AT it_mseg INTO wa_mseg.

READ TABLE it_mbew INTO wa_mbew WITH KEY matnr = wa_mseg-matnr.

CHECK sy-subrc = 0.

IF wa_mseg-menge LT wa_mbew-lbkum.

ADD wa_mseg-menge TO sum.

wa_mseg-sum = sum.

MODIFY it_mseg FROM wa_mseg.

WRITE : / wa_mseg-matnr,wa_mseg-menge,wa_mseg-sum.

ELSE.

DELETE it_mseg FROM wa_mseg.

ENDIF.

ENDLOOP.

I just hard-coded some materials for checking with the logic.You can get materials list directly from mseg as per your selection criterian right!

Read only

Former Member
0 Likes
3,592

Hi,

Try this logic.....

D

ATA: BEGIN OF it OCCURS 0,
        matnr TYPE i,
        qty   TYPE i,
      END OF it.

data: sum type i.

MOVE: 100 TO it-matnr,
      1000 TO it-qty.
APPEND it.
clear it.

MOVE: 200 TO it-matnr,
      2000 TO it-qty.
APPEND it.
clear it.

MOVE: 300 TO it-matnr,
      1500 TO it-qty.
APPEND it.
clear it.

LOOP AT it.
ADD it-qty TO sum.
WRITE:/ it-matnr, it-qty, sum.
clear it.
ENDLOOP.

Regards,

Shankar.