Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Carrying out a SUMIF like operation using SQL Server Report Builder

Tags:

I'm trying to produce a conditional sum in SQL Server Report Builder 3.0.

My expression looks like this:

=Sum(Iif(Fields!ProjectTypeID.Value=2,Fields!kWp.Value,0)) 

I'd hoped that this expression would produce a sum of the kWp of all projects of type 2.

Unfortunately, it is not to be. And I can't seem to work out why. It just returns a 0 result, even though I know that there are non-zero values in the kWp column, and the column does not contain nulls.

A colleague did manage to get a positive result by replacing the

Fields!kWp.Value  

with

1 * Fields!kWp.Value 

But we have no idea why this works, and therefore, can't really trust the answer.

How can I get this conditional sum to behave itself?

like image 267
Daniel Neal Avatar asked Jun 14 '12 09:06

Daniel Neal


People also ask

How do you add a sum in Report Builder?

To add totals for a row group In the tablix data region row group area, right-click a cell in the row group area for which you want totals, point to Add Total, and then click Before or After.

How do I Sumif in SQL query?

Excels SUMIF in SQLThe the group by and over clauses specify the rows. The column is explicitly used in the <condition> that is put into the case expression. The case expression accepts different values in the when and then branches. This allows you to do the same thing as the third argument of the sumif function.

How do you sum expressions in SSRS?

You can either likely moving the calculation into the SQL statement to allow yourself to use the normal Sum function in SSRS. You could go further down the chain and create a custom code function to add the ReportItem to a running total every detail line.

Is Ssrs applied to the sum aggregate function automatically?

SSRS will automatically apply a Sum expression.


2 Answers

The data type of the column 'kWp' is Decimal so you need to either convert the default value to 0.00 or cast the column to double

 SUM(iif(Fields!ProjectTypeID.Value = 2,cdbl(Fields!kWp.Value),0.00)) 
like image 195
praveen Avatar answered Oct 15 '22 01:10

praveen


To get the sum of the kWp of all projects of type 2, the expression is as follows,

=IIf(Fields!ProjectTypeID.Value=2,sum(Fields!kWp.Value),0)  

I hope this will help u.

like image 23
Venaikat Avatar answered Oct 15 '22 01:10

Venaikat