View Full Version : How do you make an automatic price averager? (Read)

02-24-2012, 04:25 AM
I'm having a bit of an issue, in the real world there's an economy of goods and prices, an ever changing one, The average price on a jar of honey may be $3 today but in 4 days it could be $3.25, everything changes constantly, is there a way where i could do something like this with tables? I want to be able to let users put in what the think a good price for the item would be and it takes the other prices and averages them automatically and displays the price. For example,

I have listed an apple, the starting value of the apple is $3
User 1 thinks the apple is worth around $4
($4 + $3)x .5 = $3.50 (Average cost of an Apple)
This price is now displayed to the community, that the average price of an apple is $3.50, now say another user comes around,
User 2 thinks the apple is somewhere around $2.75
($4 + $3 + $2.75) x .5 = $4.87
Now, the average price of an apple that would be displayed to the community is $4.87, is there something like this that i could use on my website? Whether it is open source or not doesn't matter, something that will check the MySQL databases for previous data and average out the information/prices/data to give an end average price between the submitted prices?

I've been having quite some trouble with this issue, i'm looking to create a price guide for online items in a gaming community, is there an easy way to do this?

02-24-2012, 02:01 PM
Because you don't know how many items there will be, or how many values
are placed on those items, I would think you would have one table in your MySQL
database that stores all of them. Each time someone gives a value to an apple,
it would insert a new line. There could be 50 rows for apples, 30 rows for oranges,
and 100 rows for bananas. You query for a particular item, you know how many
rows it found, and you SUM the values ... now you can determine the average.

If you also add a column with a timestamp, you could query the table for an item
within a certain amount of time (days, months, years). You may want to know the
average price for the apples only during the past month.

That's my take on it anyhow.


02-24-2012, 02:25 PM
in your example that average goes up after the third entry even if the new entry was lower than the previous average.

You can't just divide the sum by 2 each time a new entry has come in.
Then if the next 97 entries all are $3.50 then you would have average of

(4 + 3 + 2 + 97x3.50)*.5 = 174,63.

You only need to store the number of entries along with the count of entries.

After the first 2 entries .
you will have

EntrySum = 7;
EntryCount = 2;

And then for each new entry:

EntrySum += NewEntry;

After the 3 third it would be:

EntrySum = 9.75;
EntryCount = 3;

And then the average would be EntrySum(9.75) / EntryCount(3) = 3.25.

You could for the statistics also store the highest and the lowest entry.

And use:
EntryMax = Max(EntryMax, NewEntry)
EntryMin = Min(EntryMin, NewEntry)
to set the new value

But if you also want to know the what entry was entered most times, then you will need to store each and every entry seperately.

02-24-2012, 03:33 PM
Thank you both very much, hopefully with your replies and some other information on coding this, i'll be able to get it done with-in the next couple days. Your information was very useful. Thanked, -Skill3d