Oleg - 3 months ago 35x
SQL Question

# How to use SUM along with LOOKUP function

In my SSRS report I have two tables: Losses(Table2) and Earned Premiums(Table 1), both coming from different datasets. I need to calculate ratios based on those Losses and Earned Premiums for each month and year. For that I used LOOKUP function and it works fine:

``````SUM(Fields!PaidLosses.Value) / Lookup(Fields!AccidentYearNum.Value & Fields!AccidentMonthNum.Value, Fields!YearStartRisk.Value & Fields!MonthStartRisk.Value, Fields!EarnedPremium.Value, "EarnedAllCoverages")
``````

But now I need to calculate
`TOTALS`
. Which is Total Losses for each month and year divided by Total Earned Premium for each month and year.

http://www.sqlservercentral.com/blogs/salvoz-sql/2013/05/27/sum-result-of-ssrs-lookupset-function/

I inserteded this Custome Code:

``````Function SumLookup(ByVal items As Object()) As Decimal
If items Is Nothing Then
Return Nothing
End If
Dim suma As Decimal = New Decimal()
Dim ct as Integer = New Integer()
suma = 0
ct = 0
For Each item As Object In items
suma += Convert.ToDecimal(item)
ct += 1
Next
If (ct = 0) Then return 0 else return suma
End Function
``````

And now I am using this expression:

``````=SUM(Fields!PaidLosses.Value) / Code.SumLookup(LookupSet(Fields!AccidentYearNum.Value & Fields!AccidentMonthNum.Value, Fields!YearStartRisk.Value & Fields!MonthStartRisk.Value, Fields!EarnedPremium.Value, "EarnedAllCoverages"))
``````

But that doesnt give me correct answer for some reason.

What am I missing here?

``````=SUM(Fields!PaidLosses.Value) / Code.SumLookup(LookupSet(Fields!AccidentYearNum.Value, Fields!YearStartRisk.Value, Fields!EarnedPremium.Value, "EarnedAllCoverages"))