Conditional Sum up in SQL Server Reporting Service

时间:2016-07-11 21:35:00

标签: sql-server reporting-services

I only have two records in the table. One is Fields!Type.Value="**Buy**", and Fields!Type.Value=59. The other one is Fields!Type.Value="**Sell**", and Fields!Type.Value=59. Here I want to do conditional sum up (if it is "Sell", subtract from the total), so the result I expect is 0. I used the following formula, but it sums up the two rows, and I got 118. Why?

=sum(iif(Fields!Type.Value="Sell",-1*Fields!Shares.Value,Fields!Shares.Value))

1 个答案:

答案 0 :(得分:1)

Revised answer

My preferred option would be to modify the data source on the SQL side, so we can have a new field with the sign set correctly for each row.

Add this as a new column in your select statement:

CASE WHEN Type = 'Sell' THEN - 1 * share ELSE share END as ValueToSumInReport

Note you will need to modify to use your actual field names. Add the field to your report as

=SUM(Fields!ValueToSumInReport.Value)