Ssrs

在 SSRS 2012 中查找計算欄位的中位數

  • March 24, 2014

我有一個開始日期和一個結束日期,並且正在計算兩者之間的工作日:

我創建了一個名為 CountWeekDays 的計算欄位,它等於: Code.getBusinessDaysCount(Fields!date_created.Value,Fields!date_closed.Value)

我可以得到這樣的平均值: =Avg(Fields!CountWeekDays.Value)

但是,我不能以同樣的方式獲得中位數。我怎樣才能得到計算的東西的中位數?

我用來獲取工作日計數的程式碼如下:

Function getBusinessDaysCount(ByVal tFrom As Date, ByVal tTo As Date) As Integer
Dim tCount As Integer
Dim tProcessDate As Date = tFrom
For x as Integer= 1 To DateDiff(DateInterval.Day, tFrom, tTo) + 1
 If Not (tProcessDate.DayOfWeek = DayOfWeek.Saturday Or tProcessDate.DayOfWeek = DayOfWeek.Sunday) Then
   tCount = tCount + 1
 End If
 tProcessDate = DateAdd(DateInterval.Day, 1, tProcessDate)
Next
Return tCount

結束功能

我找到了答案的程式碼:

'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

引用自:https://dba.stackexchange.com/questions/61185