I need to find the median price of a list in a table, but the table contains
the following fields:
NumberSold - The number of items sold at a price
Price - The price of the item
I can find the median Price, but I need to find the median price of a sold
unit.
Logically I think I need to sort the list by Price, calculate the sum of
NumberSold and divide by 2 (the median datapoint), create a running total of
the NumberSold select the Price for the record at the median datapoint and
return this value.
I can sort, sum and create a running total, but I can't firgure out how to
return the Price of the median datapoint (do I use DLookup in some form?).
Please help!
I know there is some extra programming if the median datapoint is
fractional, but if anyone can help with the first part I am sure I can figure
out the rest.
Thanks!
Tom Wickerath - 03 Jan 2007 05:09 GMT
Hi Dave,
Try working through this example and see if it helps:
How to Use Code to Derive a Statistical Median
http://support.microsoft.com/kb/210581
Tom Wickerath
Microsoft Access MVP
http://www.access.qbuilt.com/html/expert_contributors.html
http://www.access.qbuilt.com/html/search.html
__________________________________________
> I need to find the median price of a list in a table, but the table contains
> the following fields:
[quoted text clipped - 19 lines]
>
> Thanks!
Tim Ferguson - 04 Jan 2007 17:51 GMT
> NumberSold - The number of items sold at a price
> Price - The price of the item
>
> I can find the median Price, but I need to find the median price of a
> sold unit.
You need to find the total number of sold units, then get the middle one.
Bear in mind that this is air code, untested, etc etc but it's not
exactly complicated...
dim totalSales as long
dim jetSQL as string
dim rst as DAO.Recordset
dim numberSoFar as Long
dim finalAnswer as Double ' or currency if you prefer
totalSales = DSum("NumberSold", "Sales", True)
jetSQL = "select Numbersold, price " & _
"from Sales " & _
"order by Price ASC"
set rst = db.openrecordset(jetSQL, dbOpenSnapshot, dbForwardOnly)
numberSoFar = 0
finalAnswer = 0
Do While finalAnswer = 0
' we should never get to the end of the recordset unless
' it was an empty table to begin with
If rst.EOF then err.raise somethingUseful ' oops!
numberSoFar = numberSoFar + rst("NumberSold")
if numberSoFar * 2 > totalSales +1 then
' if we've gone past, we don't need to worry about interpolating
finalAnswer = rst("Price")
elseif numberSoFar * 2 = totalSales + 1 Then
' okay, it's exactly the middle one out of an odd number
' so we've got the answer
finalAnswer = rst("Price")
elseif numberSoFar * 2 = totalSales Then
' this is the first of the middle pair; we need to
' average this price and the next one
finalAnswer = rst("Price")
rst.MoveNext ' need to check for error here... :-)
finalAnswer = (finalAnswer + rst("Price") / 2)
else
' no joy yet, just get the next record and move on
rst.MoveNext
end if
' return FinalAnswer...
Hope that helps
Tim F
DaveW - 05 Jan 2007 01:23 GMT
Tim:
Thanks, I guess first of all I must admit that I am a novice, so I cannot
pretend to exactly understand all the code suggested is doing... but
First I got a "Do without Loop" error, which I corrected by adding Loop to
the end of the code (makes sense to me).
But now I get an error "Object Required" on the line beginning "Set rst =..."
I am in too far over my head to figure out what to do next!
Thanks for your continued help! I don't know what I would do without this
resource!
Dave.
> > NumberSold - The number of items sold at a price
> > Price - The price of the item
[quoted text clipped - 58 lines]
>
> Tim F
Tom Wickerath - 05 Jan 2007 02:10 GMT
Hi Dave,
I tried out Tim's code this morning, and found that I needed to add:
Dim db as DAO.Database
Set db = CurrentDB()
in addition to the Loop statement that you found. Add these two lines prior
to the line that attempts to set the recordset, ie.:
set rst = db.openrecordset(jetSQL, dbOpenSnapshot, dbForwardOnly)
However, with my test data, the value returned with Tim's code (add either a
debug.print finalAnswer or MsgBox finalAnswer to see the result), was not
correct.
The solution is going to require converting your existing set of data into
one record for each entry, for example:
Price
10
15
15
15
20
20
20
20
20
20
and then apply the KB code method. However, I don't have the time available
right now to work on this, until later on Sunday evening.
Tom Wickerath
Microsoft Access MVP
http://www.access.qbuilt.com/html/expert_contributors.html
http://www.access.qbuilt.com/html/search.html
__________________________________________
> Tim:
>
[quoted text clipped - 75 lines]
> >
> > Tim F
John Nurick - 05 Jan 2007 06:51 GMT
>However, with my test data, the value returned with Tim's code (add either a
>debug.print finalAnswer or MsgBox finalAnswer to see the result), was not
>correct.
FWIW Tim's code gave the correct answer with my test data, and I can't
see anything wrong with the logic.
--
John Nurick [Microsoft Access MVP]
Please respond in the newgroup and not by email.