我有一个 SQL Server Reporting Services 报告,其中我在自定义代码块中使用 ArrayList 计算中位数,因为中位数不是 SSRS 中的内置函数。中位数和平均值显示在柱形图中。该报告包括对详细报告的深入分析。如果我深入到详细报告,然后点击浏览器中的后退箭头导航回主报告,则不会计算主报告的中位数。柱形图的那一列是空的,而平均值则按原样显示。
有没有人对为什么会发生这种情况或如何解决它有任何想法?
代码如下。AddValue() 首先创建一个新的 SortedList,它还不存在。然后它将 newValue 添加到由传递给函数的字符串作为键的 ArrayList 中,如果它以前不存在,则创建一个。每个 ArrayList 代表一组不同的值,我们要为其计算中位数。
GetMedian() 计算传递字符串标识的 ArrayList 的中值。GetMedian() 用于图表中列的值。
Public Dim ClearanceList As System.Collections.SortedList
Function AddValue(ByVal whichList As String, ByVal newValue As Integer) As Decimal
Dim thisList As System.Collections.ArrayList
If (ClearanceList is Nothing) Then
ClearanceList = New System.Collections.SortedList
End If
If (ClearanceList.ContainsKey(whichList)) Then
'Get a reference to the desired ArrayList and add the new value to it.
thisList = ClearanceList.GetByIndex(ClearanceList.IndexOfKey(whichList))
Else
thisList = New System.Collections.ArrayList()
' Create a new element in the SortedList
ClearanceList.Add(whichList, thisList)
End If
thisList.Add(newValue)
AddValue = thisList.Count
End Function
Function GetMedian(ByVal whichList As String) As Decimal
Dim thisList As System.Collections.ArrayList
Dim count As Integer
thisList = ClearanceList.GetByIndex(ClearanceList.IndexOfKey(whichList))
count = thisList.count
If (count > 0) Then
thisList.Sort()
If count Mod 2 = 1 Then
GetMedian = thisList((count - 1) / 2)
Else
GetMedian = (thisList((count / 2) - 1) + thisList((count / 2))) / 2
End If
Else
GetMedian = -1
End If
End Function