我有一个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