简体   繁体   中英

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.

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. 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 .

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.

在此处输入图像描述

SSRS tries to evaluate both side of the iif condition. So if if the numerator or the denominator is 0, then you get the error!

What you need to do is use a function to return a 0 if one of the values is a 0.

Go to report properties -> Code and then paste the following code in it custom code section

    '  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. 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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