Find the median of a calculated field in SSRS 2012

孤人 提交于 2019-12-02 08:19:15

I found code for median"

'MEDIAN code
'code from http://stackoverflow.com/questions/1943437/mean-median-mode-in-sql-server-reporting-services

Dim values As New System.Collections.Generic.List(Of Integer)
Dim valueCounts As New System.Collections.Generic.Dictionary(Of Integer, Integer)

Function AddValue(newValue As Integer) As Integer
values.Add(newValue)
AddValue = newValue
If Not valueCounts.ContainsKey(newValue) Then
    valueCounts.item(newValue) = 1
Else
    valueCounts.item(newValue) += 1
End If
End Function

Function GetMedian() As Double
Dim count As Integer = values.Count
If count = 0 Then
    Return 0
Else
    values.Sort()
    If count Mod 2 = 1 Then
        Return values(CInt((count / 2) - 0.5))
    Else
        Dim index1 As Integer = count \ 2
        Dim index2 As Integer = index1 - 1

        Dim value1, value2 As Integer
        value1 = values(index1)
        value2 = values(index2)

        Return (value1 + value2) / 2
    End If
End If
End Function
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!