简体   繁体   English

SSRS 2008r2在具有表达式的字段上使用聚合

[英]SSRS 2008r2 Using Aggregate on field with an Expression

SSRS 2008r2 SSRS 2008r2

I'm trying to perform an aggregate (Sum) on a field within a GROUPing that contains an expression. 我正在尝试在包含表达式的GROUPING内的字段上执行汇总(总和)。

The field where I want to SUM to appear is within a different GROUPing. 我希望SUM出现的字段位于不同的分组中。

I've created the following Function within Report Properties 我在报表属性中创建了以下函数

Dim public tot_OT_Hrs As Decimal 

Public Function Add_OT_Hrs(ByVal OT_Hrs As Decimal) AS Decimal 

    tot_OT_Hrs = tot_OT_Hrs + OT_Hrs 

    return OT_Hrs 

End Function 

Public Function GetTotal() 
    return tot_OT_Hrs 
End Function

I've added a call to the "Add_OT_Hrs" function in the field where the expression is and this works fine. 我在表达式所在的字段中添加了对“ Add_OT_Hrs”函数的调用,此方法运行良好。

=Code.Add_OT_Hrs(
    IIF(Sum(Fields!HrsWorked.Value) > Parameters!StdWorkingHrs.Value, 
        Sum(Fields!HrsWorked.Value) - Parameters!StdWorkingHrs.Value + Sum(Fields!Rate1Hrs.Value)
            , Sum(Fields!Rate1Hrs.Value ) 
        )
    )

Fields!Rate1Hrs.Value is the field in which the expression resides in and Fields!HrsWorked.Value is an adjacent field Fields!Rate1Hrs.Value是表达式所在的字段,而Fields!HrsWorked.Value是相邻字段

However, the field where I want to total to appear I've added the following 但是,我要总计显示的字段添加了以下内容

=Code.GetTotal()

All that is returned here is 0 (zero) on every row in the GROUPing. 返回的所有内容在GROUPING的每一行均为0(零)。 If I initialise the Dim public tot_OT_Hrs As Decimal variable to say 1.2 then 1.2 is returned on every row in the GROUPing. 如果我将Dim public tot_OT_Hrs As Decimal变量初始化为1.2,则在GROUPING的每一行上返回1.2。 The Add_OT_Hrs function isn't working as expected. Add_OT_Hrs函数无法正常工作。

屏幕截图

Where am I going wrong? 我要去哪里错了?

Thanks in advance. 提前致谢。

Looks to me that you are converting in the wrong order and also to the wrong data type. 在我看来,您转换的顺序错误并且数据类型也错误。 In the code example that you used you were converting the sum to a double which is not the correct data type. 在您使用的代码示例中,您将总和转换为不是正确数据类型的double。 There is a notable difference between a double and decimal when using custom functions. 使用自定义函数时,双精度和十进制之间存在显着差异。

I tested this with a very small dataset as follows 我用一个非常小的数据集对此进行了测试,如下所示

select '1.2'
union all 
select '1.8'

Then applied the following custom code 然后应用以下自定义代码

Dim public total as decimal
Public Function AddTotalr(ByVal r AS decimal) AS decimal
            total = total + r
            return r

End Function

Public Function GetTotalr()
            return total
End Function

The order of your explicit conversion is vital. 明确转换的顺序至关重要。 You have to convert the value before you sum the total so that you are working with consistent data types. 在对总数求和之前必须转换该值,以便使用一致的数据类型。

=code.addtotalr(sum(cdec(Fields!ID.Value)))

在此处输入图片说明

Even with the correct data type the this will produce an error because you are attempting to sum what is, most likely, being interpreted as a character string. 即使使用正确的数据类型,这也将产生错误,因为您试图将最有可能被解释为字符串的内容相加。

=code.addtotalr(cdec(sum(Fields!ID.Value)))

在此处输入图片说明

-----------edit ------------------------ -----------编辑------------------------

As a test to see what the return on the function is change your function to 作为测试,看看函数的收益是什么,将函数更改为

Dim public total as decimal
Public Function AddTotalr(ByVal r AS decimal) AS decimal
        total = total + r
        return total

This should give you a running total of the items processed and the last value in the list should match what your total should be. 这应该为您提供了已处理项目的运行总计,并且列表中的最后一个值应该与您的总计应该匹配。

在此处输入图片说明

I decided to do this in the proc. 我决定在proc中执行此操作。 It works out quite well now. 现在效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM