我希望你没事。我正在坚持使用中位代码,每次运行它时,我都会得到答案:
参数太少,预期为7。
这是我输入的内容:
Dmedian("[TTF]"; "[Filled Unrestricted TTF Values]")
或
Dmedian("TTF"; "Filled Unrestricted TTF Values")
我的表格反映了特定国家/地区特定对象的TTF(数字)。我编写了一个查询,对TTF的中位数及其平均值进行分类,并显示各国的结果。显然很简单......
以下是代码:
Function DMedian(expr As String, domain As String, Optional criteria As String) As Double
Dim dbs As Database
Dim rst As Recordset
Dim middleIndex As Integer
Dim numberOfRecords As Integer
Dim strsql As String
Set dbs = CurrentDb
If Len(criteria) <> 0 Then
strsql = "SELECT " & expr & " FROM " & domain & " WHERE " & criteria & " ORDER BY " & expr & ";"
Else
strsql = "SELECT " & expr & " FROM " & domain & " ORDER BY " & expr & ";"
'Make sure the spaces inside the quotes are preserved, otherwise your SQL will
'not be syntactically correct and Access will complain!
End If
Set rst = dbs.OpenRecordset(strsql)
If rst.BOF Then
numberOfRecords = 0
Else
rst.MoveLast
numberOfRecords = rst.RecordCount
'You need the Movelast to get the correct record count out of a recordset
End If
If numberOfRecords = 0 Then
DMedian = 0
'We assume that the median is 0 when the number of records is zero
ElseIf numberOfRecords = 1 Then
DMedian = rst(expr)
'If the number of records is 1, the value of the expression in that record is the median
Else
middleIndex = Int(0.5 * (numberOfRecords - 1) + 1)
'MiddleIndex now points to the middle of the recordset if the recordset contains an odd number of records.
'If the recordset has an even number of records, it points just above the midpoint of the recordset.
rst.MoveFirst
rst.Move (middleIndex - 1)
DMedian = rst(expr)
'if the number of records is odd, we are done
If numberOfRecords Mod 2 = 0 Then
'the number of records is an even number
rst.MoveNext
DMedian = (DMedian + rst(expr)) / 2#
'Take the average between the values in this record and the next record
'Note that the next record will point just below the midpoint of the recordset
End If
End If
rst.Close
Set rst = Nothing
dbs.Close
Set dbs = Nothing
'Cleanup everything before leaving the function
End Function
我确实添加了&#39;&#39;在SQL代码中但结果是一样的。
你有什么想法吗?