SSRS 中的惰性评估

发布于 2024-07-10 22:07:37 字数 342 浏览 18 评论 0原文

我正在使用 SSRS 2005 生成报告,报告中的一列是简单的均值计算。 我不想除以零,所以对于我输入的文本框值:

=Switch(Fields!Count.Value=0,0,Fields!Count.Value>0,Fields!Sum.Value/Fields !Count.Value)

这仍然计算第二个表达式。

也是如此:

=IIF(Fields!Count.Value=0,0,Fields!Sum.Value/Fields!Count.Value)

我不希望我的报告显示错误。 我怎样才能克服这个问题?

I'm using SSRS 2005 to produce a report, and one of the columns in my report is a simple mean calculation. I don't want to divide by zero, so for the textbox value I have put:

=Switch(Fields!Count.Value=0,0,Fields!Count.Value>0,Fields!Sum.Value/Fields!Count.Value)

This still evaluates the second expression.

And so does:

=IIF(Fields!Count.Value=0,0,Fields!Sum.Value/Fields!Count.Value)

I don't want my report to display errors. How can I overcome this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

君勿笑 2024-07-17 22:07:37

不幸的是,因为 IIF 实际上只是一个函数,所以所有参数在调用函数之前都会被计算(导致除以零)。

在表达式中嵌入复杂逻辑的一种方法是在报表中嵌入函数。 您可以编写一个 VB 函数来返回“报告属性”的“代码”选项卡中您喜欢的任何内容。

Public Function GetMeanValue(ByVal Sum as Decimal, ByVal Count As Int) As Decimal
    'your logic in plain old vb syntax here

End Function

在文本框文本表达式属性中:

=Code.GetMeanValue(Fields!Sum.Value, Fields!Count.Value)

Unfortunately because IIF is actually just a function, all arguments get evaluated before the function gets called (resulting in your divide by zero).

One way of embedding complex logic in an expression is to embed a function in the report. You can write a VB function to return whatever you like in the Code Tab of the Report Properties.

Public Function GetMeanValue(ByVal Sum as Decimal, ByVal Count As Int) As Decimal
    'your logic in plain old vb syntax here

End Function

In the Textbox Text expression property:

=Code.GetMeanValue(Fields!Sum.Value, Fields!Count.Value)
倾听心声的旋律 2024-07-17 22:07:37

检查零时,我总是对表达式求反,如下所示:

=IIF(Fields!Count.Value<>0,Fields!Sum.Value/Fields!Count.Value,0)

这样做的另一个好处是,可以在 Count 字段为空/null 的情况下避免除法。

When checking for zeros, I always negate the expression like this:

=IIF(Fields!Count.Value<>0,Fields!Sum.Value/Fields!Count.Value,0)

This has the added benefit of avoiding the division in case the Count field is empty/null.

静赏你的温柔 2024-07-17 22:07:37

试试这个:

=IIf(Fields!Count.Value = 0, 0, Fields!Sum.Value / IIf(Fields!Count.Value = 0, 1, Fields!Count.Value))

Try this:

=IIf(Fields!Count.Value = 0, 0, Fields!Sum.Value / IIf(Fields!Count.Value = 0, 1, Fields!Count.Value))

瑶笙 2024-07-17 22:07:37

这是一直对我有用的解决方案:
避免除以零错误

如果报告有太多部门,就会变得乏味。 我更喜欢一些VB代码来进行除法并处理除零。 不确定过多的 vb 代码是否会降低报告的性能。

这是 如何用 vb 做到这一点
将 he 链接替换为 archive.org 链接。 以下是该链接的相关内容:

不要将该计算放入所有文本框中
做师我去下报告>> 报告属性>> 代码和
放入以下代码:

公共共享函数除法(Num1为双精度,Num2为双精度)AS
对象

 IF ISNOTHING(Num2) 或 Num2 = 0 那么 
          除法=“不适用” 
      ELSEIF Num1 = 0 那么 
          除= 0 
      别的 
          除法 = Num1 / Num2 
      万一 
  结束功能 
  

然后在报告上我需要做的每个文本框中
除法,我这样称呼该函数:

=Code.Divide(1, 0) 
  

Here is solution that has always worked for me:
Avoiding Divide By Zero Errors .

It gets tedious if the report has too many divisions. I prefer some vb code to do the division and handle division by zero. Not sure if too much of vb code would bring down the performance of the report.

Here is how to do it with vb
Replaced he link with archive.org link. Here is the relevant content from that link:

Instead of dropping that calculation into all the textboxes that are
doing division I go under Report >> Report Properties >> Code and
drop in the following code:

Public Shared Function Divide(Num1 as double, Num2 as double) AS
object

    IF ISNOTHING(Num2) Or Num2 = 0 Then
        Divide = "n/a"
    ELSEIF Num1 = 0 THEN
        Divide = 0
    ELSE
        Divide = Num1  / Num2
    END IF
End Function

And then in each of the textboxes on the report where I need to do
division, I call the function like this:

=Code.Divide(1, 0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文