我正試圖在Access中找到更快速的方式來計算我的中位數。您可以在下面的代碼中查看一次查詢一個項目代碼的位置,進行排序,然後計算中位數。有時候有600個物品代碼,這些物品可能每個都有1000多個與其相關的基地。例如,我正在使用的特定表格總共有15萬條記錄,並且速度非常慢。有沒有更好的方法來一次計算每個記錄中位數,而不是一次計算一個項目代碼。MS Access VBA:無需迭代記錄即可計算中位數
Function FIncPercentile(ByVal posCode As Single, ByVal k As Single, ByVal tbl As String) As Variant
Dim rstRec As Recordset
Dim db As Database
Dim n As Integer
Dim i As Single
Dim res, d1, d2 As Currency
' Create recordset from query
Set db = CurrentDb
Set rstRec = db.OpenRecordset("SELECT Co, Base " & _
"FROM " & tbl & " " & _
"WHERE Code = " & pos & " " & _
"ORDER BY Base ASC;")
' Skip if there are no matches
If IsNull(rstRec!base) Or rstRec.RecordCount = 0 Then
FBasePercentile = Null
Exit Function
End If
' Count records
rstRec.MoveLast
n = rstRec.RecordCount
rstRec.MoveFirst
' Calculate the index where k is the percentile
i = n * k
' Test the decimal and find value accordingly
If i = Int(i) Then
rstRec.Move i - 1
d1 = rstRec!base
rstRec.MoveNext
d2 = rstRec!base
FIncPercentile = (d1 + d2)/2
Else
i = Round(i + 0.5, 0)
rstRec.Move i - 1
FIncPercentile = rstRec!base
End If
End Function
你嘗試打開一個用於記錄通過共,基部和通過每個共同在外環計算與濾波器中值在一個內部循環迭代下令每個表? – Beth
您可能可以調整微軟網站上的DMedian函數和示例https://msdn.microsoft.com/en-us/library/dd789431%28v=office.12%29.aspx - 或者是您獲得代碼的地方? – dbmitch
有多少個唯一的表 - 如果只有一個 - 我認爲你會看到立即增加,通過在代碼是參數的表上創建參數查詢。使用參數querydef創建記錄集 - 而不是動態SQL - 並確保您在代碼和Base上都有索引。 – dbmitch