Hi, I am using the function below to find the median value of records but the problem is that I am trying to group by multi-levels in a query. For example, I have three other fields "Account_Name", "Claim_Status", "Disease_Category" that I need to take into account when calculating median value. I found it easy to get an average value of the records via total row feature in query design but there is nothing for median. Here is the SQL view of my query.
I want the Median_Indemnity to have the same functionality as the field "AvgOfIndemnity_Paid".
Any help would be greatly appreciated
SELECT FINAL_FOR_DB.Account_Name, Count(FINAL_FOR_DB.Claim_Status) AS CountOfClaim_Status, FINAL_FOR_DB.Claim_Status, Avg(FINAL_FOR_DB.Indemnity_Paid) AS AvgOfIndemnity_Paid, MedianF("FINAL_FOR_DB","Indemnity_Paid") AS Median_Indemnity, FINAL_FOR_DB.Disease_Category
FROM FINAL_FOR_DB
GROUP BY FINAL_FOR_DB.Account_Name, FINAL_FOR_DB.Claim_Status, MedianF("FINAL_FOR_DB","Indemnity_Paid"), FINAL_FOR_DB.Disease_Category
HAVING (((FINAL_FOR_DB.Claim_Status)="SETTLED"));
Function MedianF(pTable As String, pfield As String, Optional pgroup As String, Optional pgroup2 As String) As Single
'*******************************************
'Purpose: Return median value from a recordset
'Coded by: raskew
'Inputs: ? medianF("Orders", "Freight",[Value of Grouping field]) <enter>
'Output: 41.36 (may vary according to how much
' you've fiddled with this table).
'*******************************************
Dim rs As Recordset
Dim strSQL As String
Dim n As Integer
Dim sglHold As Single
If Len(pgroup) > 0 Then
strSQL = "SELECT " & pfield & " from " & pTable & " WHERE fieldname1= '" & pgroup & "' and fieldname2='" & pgroup2 & "' and " & pfield & ">0 Order by " & pfield & ";"
Else
strSQL = "SELECT " & pfield & " from " & pTable & " WHERE " & pfield & ">0 Order by " & pfield & ";"
End If
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
I want the Median_Indemnity to have the same functionality as the field "AvgOfIndemnity_Paid".
Any help would be greatly appreciated
SELECT FINAL_FOR_DB.Account_Name, Count(FINAL_FOR_DB.Claim_Status) AS CountOfClaim_Status, FINAL_FOR_DB.Claim_Status, Avg(FINAL_FOR_DB.Indemnity_Paid) AS AvgOfIndemnity_Paid, MedianF("FINAL_FOR_DB","Indemnity_Paid") AS Median_Indemnity, FINAL_FOR_DB.Disease_Category
FROM FINAL_FOR_DB
GROUP BY FINAL_FOR_DB.Account_Name, FINAL_FOR_DB.Claim_Status, MedianF("FINAL_FOR_DB","Indemnity_Paid"), FINAL_FOR_DB.Disease_Category
HAVING (((FINAL_FOR_DB.Claim_Status)="SETTLED"));
Function MedianF(pTable As String, pfield As String, Optional pgroup As String, Optional pgroup2 As String) As Single
'*******************************************
'Purpose: Return median value from a recordset
'Coded by: raskew
'Inputs: ? medianF("Orders", "Freight",[Value of Grouping field]) <enter>
'Output: 41.36 (may vary according to how much
' you've fiddled with this table).
'*******************************************
Dim rs As Recordset
Dim strSQL As String
Dim n As Integer
Dim sglHold As Single
If Len(pgroup) > 0 Then
strSQL = "SELECT " & pfield & " from " & pTable & " WHERE fieldname1= '" & pgroup & "' and fieldname2='" & pgroup2 & "' and " & pfield & ">0 Order by " & pfield & ";"
Else
strSQL = "SELECT " & pfield & " from " & pTable & " WHERE " & pfield & ">0 Order by " & pfield & ";"
End If
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