Application Development 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: 

logic to calculate the average price

former_member342013
Contributor
0 Kudos
232

hi,

for one material i will be having N number of net price values it depends

for some material 2 values and some material it may be more...

i want to sum up all these values for all material then i have to divide by the number of materials

how to do this?

Ex : 

Matria     netprice
11111    10
              20
22222    30
              40

in above example i want to sum up 10 and 20 for 11111 material and i wil divide by 2, again for 2nd material 22222 i wil sum up 30 and 40 and i wil divide by 2

how to do this?

9 REPLIES 9

Former Member
0 Kudos
94

Hi,

This is very simple...follow the below steps:

1. sort the internal table on the basis of Material

2.loop at the internal table

3. Take 3 local variables

Count That will be keep the track of no of netprice

Sum that conatins the sum of all the net price corresponding to the material

Avg is the sum/count

4.USe

AT NEW Material

set count = 0.

endat

5.

Outside AT NEW OF loop

count = count + 1.

and SUm = SUM + netprice

6.AT END OF Material

Avg = sum/count

endat

append the value to an unternal table or display as it is...depending upon your requirements.

Hope this might help you out.

Pooja

Former Member
0 Kudos
94

sorry for repost

Edited by: Pooja Gupta on Aug 28, 2009 1:10 PM

Former Member
0 Kudos
94

dgrh

Former Member
0 Kudos
94

Hi,

What is the internal table structure you are using to store those values? Particularly for netprice

Regards,

Vikranth

Former Member
0 Kudos
94

Please confirm.

are your internal table having the entries like this?

Material Number Net Price

1111 10

1111 20

2222 30

3333 40

Or what is the structure of your internal table.

0 Kudos
94

yes that is only the structure of my internal table

Former Member
0 Kudos
94

HI,

Well then try something like this,



loop at itab.
v_ind = sy-tabix - 1.
read table itab into wa index v_ind.
if sy-subrc = 0.
if itab-matnr = wa-matnr.
if sy-tabix = 1.
sum = itab-netpr + wa-netpr.
else.
sum = sum + wa-netpr.
count = count + 1.
endif.
else.
continue.
endif.
avg = sum / count.

write: avg.
endif.
endloop.

Regards,

Vik

Former Member
0 Kudos
94

Just sort the table by material number

Try using on change of material number this way you don't have to go for a lenghty code \

it is like

sort itab by matnr.

loop at itab into wa.

on change of wa-matnr.

add the price.

clear price.

endloop.

The problem here is that on change of wa_matnr will not get triggered for the last material number i,e when you exit the loop so append it outside the loop.

Former Member
0 Kudos
94

Try like this

DATA: begin of itab OCCURS 0,

matnr like mara-matnr,

dmbtr like bseg-dmbtr,

end of itab.

data: var like bseg-dmbtr,

var1 like bseg-dmbtr,

counter type i.

itab-matnr = 'abcdef'.

itab-dmbtr = '1111'.

append itab.

itab-matnr = 'abcdef'.

itab-dmbtr = '1111'.

append itab.

itab-matnr = 'ghijkl'.

itab-dmbtr = '2222'.

append itab.

itab-matnr = 'ghijkl'.

itab-dmbtr = '2222'.

append itab.

loop at itab.

counter = counter + 1.

var = var + itab-dmbtr.

at end of matnr.

var1 = var / counter.

clear: var,

counter.

write: var1.

ENDAT.

endloop.

Hope this will help you.