Hello
In a previous post from a few years ago (161348) I had some assistance from PHV and Skip on getting a query to work for percentiles but then the project got dropped so I didn't persue. But now I'm back at it so hopefully someone can help.
I have a data query where each line of data is one hospital visit. I have unique ID, visit date, triage level and ED LOS.
What I want is to have a query that will show each admit date with the 90th percentile LOS for each triage level for that date. Percentile value isn't necessarily an actual value in the list, but a data point representing the 90th percentile in the same way Excel calculates.
What I have so far (written by PHV) is code in a module for percentile:
In the query itself I have the code:
However, this requests input of dates and triage levels and I just want an entire list grouped by date and triage. Thanks.
In a previous post from a few years ago (161348) I had some assistance from PHV and Skip on getting a query to work for percentiles but then the project got dropped so I didn't persue. But now I'm back at it so hopefully someone can help.
I have a data query where each line of data is one hospital visit. I have unique ID, visit date, triage level and ED LOS.
What I want is to have a query that will show each admit date with the 90th percentile LOS for each triage level for that date. Percentile value isn't necessarily an actual value in the list, but a data point representing the 90th percentile in the same way Excel calculates.
What I have so far (written by PHV) is code in a module for percentile:
Code:
Public Function XthPercentile(SQL As String, ColName As String, X As Double) As Variant
Dim Percentile As Double
Dim XthRec As Double
Dim iRec As Long
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset(SQL, dbOpenDynaset)
With rst
If Not (.BOF Or .EOF) Then
.MoveLast
If X = 1 Then Percentile = .Fields(ColName)
.MoveFirst
If X = 0 Then Percentile = .Fields(ColName)
If X > 0 And X < 1 Then
XthRec = 1 + X * (.RecordCount - 1)
iRec = Int(XthRec)
.Move iRec - 1
Percentile = .Fields(ColName)
XthRec = XthRec - iRec
If XthRec > 0 Then
.MoveNext
Percentile = Percentile + XthRec * (.Fields(ColName) - Percentile)
End If
End If
XthPercentile = Percentile
End If
.Close
End With
Set rst = Nothing
End Function
In the query itself I have the code:
Code:
SELECT A.AdmDate AS Expr1, A.Triage AS Expr2, XthPercentile("SELECT LOS FROM qryEDLOS WHERE AdmDate=#" & A.AdmDate & "# AND Triage='" & A.Triage & "' ORDER BY LOS","LOS",0.5) AS [90th Percentile]
FROM qryEDLOS
GROUP BY A.AdmDate, A.Triage;
However, this requests input of dates and triage levels and I just want an entire list grouped by date and triage. Thanks.