是否可以計算一列數據的平均值,中位數,模式,標準偏差等?SQL Server Reporting Services中的平均值,中位數,模式
一般來說,是否可以在SQL Server Reporting Services中進行這些類型的數學計算?
如果是這樣,該怎麼辦?
是否可以計算一列數據的平均值,中位數,模式,標準偏差等?SQL Server Reporting Services中的平均值,中位數,模式
一般來說,是否可以在SQL Server Reporting Services中進行這些類型的數學計算?
如果是這樣,該怎麼辦?
這裏是Median()
從Report Design Tips and Tricks ...
方案1
1:在報表設計器,打開報告屬性對話框,然後單擊代碼選項卡。定義一個數組,一個接受一個值並將其添加到數組的函數,以及一個計算數組中值的函數;
Dim values As New SystemCollections.ArrayList
Function AddValue(newValue As Decimal) As Decimal
values.Add(newValue)
AddValue = newValue
End Function
Function GetMedian() As Decimal
Dim count As Integer = values.Count
If (count > 0)
values.Sort()
GetMedian = values(count\2)
End If
End Function
2:將函數的調用放入聚合中,並將其添加到詳細信息行中的表達式中。
=Max(Code.AddValue(Fields!field.Name))
3:從表中的頁腳文本框,調入GetMedian()來檢索值
=Code.GetMedian()
下面是我收到模式適合年齡:
Declare @Temp Table(Id Int Identity(1,1), Data Decimal(10,5))
Insert into @Temp Select DATEDIFF (YY, EmployeeCustomTabFields.CustDOB, GETDATE()) -
Case When (MONTH(EmployeeCustomTabFields.CustDOB)=MONTH(GETDATE()) AND DAY(EmployeeCustomTabFields.CustDOB) > DAY(GETDATE()) OR MONTH (EmployeeCustomTabFields.CustDOB) > MONTH (GETDATE()))
Then 1 Else 0 End as Age
From EM
inner join EmployeeCustomTabFields on EmployeeCustomTabFields.Employee = EM.Employee
Where EmployeeCustomTabFields.CustDepartment = '23 - Piping Design' and EM.Status = 'A' and EM.Type in ('A','B','C')
Select Top 1 with ties DATA
From @Temp
Where DATA IS Not NULL
Group By DATA
Order By COUNT(*) DESC
在@ Homer的回答中進行了擴展,下面的代碼可以用來獲取中位數和模式。我需要Integer的,但接受Decimal或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
請注意,在SSRS 2012中它是'System.Collections.ArrayList' – 2016-03-21 21:38:09