简体   繁体   English

SQL 报表生成器:如何在 SSRS 中使用 WHERE 子句的功能?

[英]SQL Report Builder: how to use functionality of WHERE clause inside SSRS?

I need to be able to do a calculation that gets a cents per gallon value.我需要能够进行计算,得到每加仑美分的价值。 This is achieved simply with PRICE / GALLONS = CPG.这可以通过 PRICE / GALLONS = CPG 轻松实现。

Since I obviously cannot use an actual WHERE clause in the reports Expression feature, the closest I could find is by using an IIF() function.由于我显然不能在报告表达式功能中使用实际的 WHERE 子句,因此我能找到的最接近的是使用 IIF() function。 However, I'm not quite getting what I want to see.但是,我并没有完全得到我想看到的东西。

Here's the expression:这是表达式:

=IIF(Fields!gallons.Value=0,"-", Fields!GP.Value/Fields!gallons.Value)

As you can see, the report works for the values that don't have a zero for their gallons value.如您所见,该报告适用于加仑值不为零的值。 But when it comes to where there are zeroes, I get the dreaded #Error .但是当涉及到零的位置,我得到了可怕的#Error

How might I instead go about handling the zeroes?我该如何处理零? It may be important to note that I can't filter out the zeroes in my SQL query beforehand because the related columns must be there for other calculations.可能需要注意的是,我无法事先过滤掉 SQL 查询中的零,因为相关列必须存在于其他计算中。

在此处输入图像描述

SSRS tries to evaluate both side of the iif condition. SSRS 尝试评估 iif 条件的两侧。 So if if the numerator or the denominator is 0, then you get the error!因此,如果分子或分母为 0,则会出现错误!

What you need to do is use a function to return a 0 if one of the values is a 0.如果其中一个值为 0,您需要做的是使用 function 返回 0。

Go to report properties -> Code and then paste the following code in it custom code section Go 报告属性 -> 代码然后将以下代码粘贴到其中自定义代码部分

    '  Fix for divide by zero problem in VB

   Public Shared Function VarPercent(ByVal Standard As Decimal, ByVal Actual As Decimal) As Decimal
If Actual = 0 Then
Return 0
End If
If Standard = 0 Then
Return 0
End If
Return (Standard / Actual )
End Function

No on your report, where ever you divide two values, use the following:否在您的报告中,无论您在哪里划分两个值,请使用以下内容:

=Code.VarPercent(Fields!GP.Value,Fields!gallons.Value)

报告属性

SSRS is dumb that way. SSRS就是那样愚蠢。 You have to fix the divide by zero anyway:无论如何,您必须修复除以零:

=IIF(Fields!gallons.Value = 0,"-",
      Fields!GP.Value / IIF(Fields!gallons.Value = 0 , 1, Fields!gallons.Value))

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

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