So Ive simplified my query to try to get something to work. Heres what it looks like right now
VBA:
Code:
Function MedianF(pDataSet As String, pField As String) As Single
Dim rs As Recordset
Dim strSQL As String
Dim n As Long
Dim sglHold As Single
strSQL = "SELECT " & pField & " FROM " & pDataSet & " WHERE " & pField & ">0 Order by " & pField & ";"
Set rs = CurrentDb.OpenRecordset(strSQL)
rs.MoveLast
n = rs.RecordCount
rs.Move -Int(n / 2)
If n Mod 2 = 1 Then 'odd number of elements
MedianF = rs(pField)
Else 'even number of elements
sglHold = rs(pField)
rs.MoveNext
sglHold = sglHold + rs(pField)
MedianF = sglHold / 2
End If
rs.Close
End Function
Query:
Code:
SELECT MedianF("Rate","TotalPremium") AS Expr2, Driver.Age, Driver.Sex, Driver.Marital
FROM ((Policy INNER JOIN Car ON Policy.RecordID = Car.PolicyLinkID) INNER JOIN Driver ON Policy.RecordID = Driver.PolicyLinkID) INNER JOIN Rate ON Policy.RecordID = Rate.PolicyLinkID
GROUP BY Driver.Age, Driver.Sex, Driver.Marital;
The code runs but it gives me the median of all the data in the TotalPremium field. I want it to give me the median of the totalpremium of all 18 Single Males, 19 single Males, 18 Married Femals etc...
I suspect I need to change something with the where clause in my code but haven't had any luck so far. Can anyone give me pointers? When I run the AVG function it works just fine.