Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
632 views
in Technique[技术] by (71.8m points)

ssrs 2008 - mean, median, mode in SQL Server Reporting Services

Is it possible to calculate an mean, median, mode, standard deviation, etc. of a column of data?

In general, is it possible to do these sorts of math calculations in SQL Server Reporting Services?

If so, how can it be done?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Expanding on @Homer's answer, the code below can be used to get both the Median and the Mode. I needed Integers but it would be a quick change to accept Decimal or Double.

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

Function GetMode() As String
    Dim max As Integer = 0
    For Each v As Integer In valueCounts.Values
        If v > max Then
            max = v
        End If
    Next v

    Dim maxCount As Integer = 0
    Dim retValue As String = ""
    For Each vcKvp As System.Collections.Generic.KeyValuePair(Of Integer, Integer) In valueCounts
        If vcKvp.Value = max Then
            maxCount += 1
            If Not String.IsNullOrEmpty(retValue) Then
                retValue &= ", "
            End If
            retValue &= vcKvp.Key
        End If
    Next vcKvp

    If maxCount = valueCounts.Count Then
        Return "N/A"
    End If

    Return retValue
End Function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...