使用报表生成器中的查找对来自不同数据集的两个值求和

时间:2012-11-01 08:32:12

标签: ssrs-2008 reporting reporting-services reportbuilder3.0

我有一份报告,应该按货币从2数据集中读取值:

Dataset1: Production Total
Dataset2: Net Total

我试过用:

Lookup(Fields!Currency_Type.Value, 
       Fields!Currency_Type1.Value,
       Fields!Gross_Premium_Amount.Value, 
       "DataSet2")

这只返回数据集2中的第一个数量。

我也尝试了Lookupset函数,但它没有对检索到的值进行求和。

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:2)

感谢Jamie的回复。 这就是我所做的,它完美无缺: 来自报告属性 - >代码,写下面的函数:

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)
Next
If (ct = 0) Then return 0 else return suma 
End Function

然后你可以调用函数:

code.SumLookup(LookupSet(Fields!Currency_Type.Value, Fields!Currency_Type1.Value,Fields!Gross_Premium_Amount.Value, "DataSet2"))

答案 1 :(得分:1)

是的,Lookup只返回第一个匹配的值。我想到了三种选择:

  1. 更改您的查询,以便您只需获取一个值:使用GROUP BYSUM(...)组合查询中的两行。如果您在其他地方使用此查询,请复制并更改它。
  2. 行中有什么不同吗?比如一个是去年,一个是今年?如果是这样,请创建一个人工查找键并分别查找这两个值:

     
    =Lookup(Fields!Currency_Type.Value & ","
        & YEAR(DATEADD(DateInterval.Year,-1,today())), 
       Fields!Currency_Type1.Value & ","
        & Fields!Year.Value,
       Fields!Gross_Premium_Amount.Value, 
       "DataSet2")
    +
    Lookup(Fields!Currency_Type.Value & ","
       & YEAR(today()), 
      Fields!Currency_Type1.Value & ","
       & Fields!Year.Value,
      Fields!Gross_Premium_Amount.Value, 
      "DataSet2")
    
  3. 如上所述使用LookupSet function。有了这个,你将获得一组值,然后需要将它们加在一起。最简单的方法是使用报表中的嵌入代码。将此函数添加到报告的代码中:

    Function AddList(ByVal items As Object()) As Double
       If items Is Nothing Then
         Return 0
       End If
    
       Total = 0
       For Each item As Object In items
          Total = Total + CDbl(item)
       Next
    
       Return Total
    End Function
    

    现在用:

    来调用它
    =Code.AddList(LookupSet(Fields!Currency_Type.Value, 
       Fields!Currency_Type1.Value,
       Fields!Gross_Premium_Amount.Value, 
       "DataSet2"))
    
  4. (注意:这段代码没有经过测试。我只是在Stack Overflow编辑窗口中编写它并且我不是VB的粉丝。但它应该让你知道该怎么做。)