Link to home
Start Free TrialLog in
Avatar of rossifumi46
rossifumi46

asked on

Percentile in Access

Hi I did a search on the web and found a code for percentile but somehow I'm not able to get it to work correctly bec of the way i've designed my db. Since my select statement involves 2 tables, i not able to use the function. Below is the table structure.

table 1:Student
StudentID Name Class

table 2: Mathematics
ExamID StudentID Scores

So basically I need to get the percentile for a given a class.

Thanks





Public Function PercentileRst(RstName As String, fldName As String, PercentileValue As Double) As Double
   'This function will calculate the percentile of a recordset.
   'The field must be a number value and the percentile has to
   'be between 0 and 1.
   If PercentileValue < 0 Or PercentileValue > 1 Then
      MsgBox "Percentile must be between 0 and 1", vbOKOnly
   End If
   Dim PercentileTemp As Double
   PercentileTemp = 0
   Dim dbs As Database
   Set dbs = CurrentDb
   Dim xVal As Double
   Dim iRec As Long
   Dim i As Long
   Dim RstOrig As Recordset
   Set RstOrig = CurrentDb.OpenRecordset(RstName, dbOpenDynaset)
   RstOrig.Sort = fldName
   Dim RstSorted As Recordset
   Set RstSorted = RstOrig.OpenRecordset()
   RstSorted.MoveLast
   RstSorted.MoveFirst
   xVal = ((RstSorted.RecordCount - 1) * PercentileValue) + 1
   'x now contains the record number we are looking for.
   'Note x may not be     whole number
   iRec = Int(xVal)
   xVal = xVal - iRec
   'i now contains first record to look at and
   'x contains diff to next record
   RstSorted.Move iRec - 1
   PercentileTemp = RstSorted(fldName)
   If xVal > 0 Then
      RstSorted.MoveNext
      PercentileTemp = ((RstSorted(fldName) - PercentileTemp) * xVal) + PercentileTemp
   End If
   RstSorted.Close
   RstOrig.Close
   Set RstSorted = Nothing
   Set RstOrig = Nothing
   Set dbs = Nothing
   PercentileRst = PercentileTemp
End Function

Open in new window

Avatar of shru_0409
shru_0409
Flag of India image

select class, (scores*count(*))/100 as percentile
from student s inner join mathematics m on (s.StudentID = m.StudentID )
where class = '&1'
group by class

try this but i m not sure .or check.............
Avatar of rossifumi46
rossifumi46

ASKER

hmm but I don't think we will be to indicate the percentile range  (e.g. 60 percentile range)
thanks shru for replying
can u sent the sample data with output....... it will help to solution
Haven't tested, but please give this a try:

select PercentileRst("Select * from Mathematics a, Student b where a.StudentID = b.StudentID", "Class", yourpercvalue), a.studentid, a.studentname, a.class from (Mathematics a
inner join Student b on a.StudentID = b.StudentID)

Open in new window

anyone can help me with this problem?
ASKER CERTIFIED SOLUTION
Avatar of ralmada
ralmada
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you ralmada,
will it possible to share your sample prog. thanks
I'm just using the function you provided with a table I've created. The syntax is the same as I indicated in my previous posting. As you can see, basically I'm passing a query instead of a table name in order to pull the records required.
sorry i might sound stupid to ask this. I did make use of the your solution
but I get this error message Compile Error Expected: Case
How do i workaround this
thanks
Can you post the query the way you're using it?
here's my query
thanks
select PercentileRst("Select * from Math a, Student b where a.Student_ID = b.Student_ID", "b.Class", 0.2), a.student_id, a.student_name, b.class from (Math a
inner join Student b on a.StudentID = b.StudentID)

Open in new window

i missed the underscores for the above code.

this should be the query
select PercentileRst("Select * from Math a, Student b where a.Student_ID = b.Student_ID", "b.Class", 0.2), a.student_id, a.student_name, b.class from (Math a
inner join Student b on a.Student_ID = b.Student_ID)

Open in new window

and also when i run the query i get this error code "undefined function PercentileRst in expression"

but when i run the PercentileRst function alone it works perfectly.
Hmm, I suspect there might be a problem in the way the function is created. Can you also post a sample database so I can check? Because I cannot reproduce your error. the function is working perfectly in my environment.
thanks ralmada.

I've attached a sample db.Btw the percentile is for the scores in a given class.
testcase---Copy---Copy2.mdb
You created the function in the wrong place. It should be created in a Module, not in the form. Check the attached DB. I've also created a query, (Query1) using the function.
testcase---Copy---Copy2.mdb
thank you so much ralmada
Avatar of Luke Chung
Here's a page that about how Percenties are calculated and assigned in our Total Access Statistics product: http://fmsinc.com/MicrosoftAccess/dataanalysis/percentiles/ 
A free demo is available that can calculate percentiles on tables in your Access database: http://fmsinc.com/Products/statistics/conftrial.asp
Total Access Statistics also performs a wide range of other numerical functions. Hope this helps.