SSRS report builder 2014 CDbl precision - reporting-services

I have some column in my report, calculated the Sum using this exepression :
=SUM(IIF(Fields!Type.Value = 0, CDbl(Fields!Amount.Value), CDbl(0.0)))
it works but gives me the sum with no precision.
is there a way I can view the Sum with precision?
UPDATE
the Fields!Amount.Value is alway decimal(18, 3)
If The calculation was like sum = 10 + 10 the result becomes 20
if sum = 10 + 2.125 the result becomes 12.125
I want the result to be converted to (18, 3).
so I expect the result in the first example to be 20.000

Your report is correctly returning the values to the necessary precision, it is just only showing the significant figures because you have not told it what number formatting you require and is making a best guess.
If you change your expression to the below, you should get your desired output:
=FORMAT(SUM(IIF(Fields!Type.Value = 0, CDbl(Fields!Amount.Value), CDbl(0.0))),"0.000")

Related

SUBSTRING_INDEX Not Warking in Mysql

I am trying to find max invoice:
SELECT IFNULL(MAX(SUBSTRING_INDEX(invoice,'I', -1)) + 1, 1) AS invoice
FROM sales
SQL Fiddle
When I run this SQL query, it can not count more than 10.
invoice
20221026P1I1
20221026P1I2
20221026P1I3
20221026P1I4
20221026P1I5
20221026P1I6
20221026P1I7
20221026P1I8
20221026P1I9
20221026P1I10
20221026P1I11
20221026P1I12
I am trying to find max invoice 12 + 1 = 13
Your use of SUBSTRING_INDEX() is correct, however you should cast the string value to a bona fide integer:
SELECT COALESCE(MAX(CAST(SUBSTRING_INDEX(invoice, 'I', -1) AS UNSIGNED)), 1) AS invoice
FROM sales;
The problem with trying to find the max of the text substrings themselves is that text numbers sort lexicographically, e.g.
1
10
11
2
23
But this isn't the behavior you want, you want the numeric maximum. Hence we should cast these substrings and then compare.
Side note: You could have avoided this problem entirely by maintaining a pure numeric invoice number column. You may want to change your table design to include such a column.

NaN Error report builder 3.0

New to SSRS. Using Report Builder 3.0. Trying to write a percentage expression.
This is the expression I have
=Sum(IIf(Fields!lastfinancialyearflag.Value=1,0,Fields!Total_Delivered.Value)) /
Sum(IIf(Fields!lastfinancialyearflag.Value=1,0,Fields!Total_Orders.Value))
I have highlighted the cell then, and formatted it to percentage, and 2 decimal places.
However I keep getting some cells showing NaN. I have read this happens when the value is 0, but I have e.g 1/4 gives me NaN error.
Is there a way to rid the NaN error, or a better way of writing the percentage expression please?
Note:
Total Orders / Total Delivered columns are 1 and 0 values
lastfinancialyearflag column is 1 and 0 values, but only want to sum where value is 1.
Please help!
You said "but only want to sum where value is 1." but your IIF expressions will return zero when lastfinancialyearflag is 1
try switch ing it round like this.
=Sum(IIf(Fields!lastfinancialyearflag.Value=1, Fields!Total_Delivered.Value, 0)) /
Sum(IIf(Fields!lastfinancialyearflag.Value=1, Fields!Total_Orders.Value, 0))
You have to make an extra check that the denominator (sum of total_orders.value) is not 0
=
Iif(
Sum(IIf(Fields!lastfinancialyearflag.Value=1,0,Fields!Total_Orders.Value))=0,
0,
Sum(IIf(Fields!lastfinancialyearflag.Value=1,0,Fields!Total_Delivered.Value))
)
/
Iif(
Sum(IIf(Fields!lastfinancialyearflag.Value=1,0,Fields!Total_Orders.Value))=0,
1,
Sum(IIf(Fields!lastfinancialyearflag.Value=1,0,Fields!Total_Orders.Value))
)
=
Iif(Sum(IIf(Fields!lastfinancialyearflag.Value=1, Fields!Total_Delivered.Value, 0))
=0
,0
,Sum(IIf(Fields!lastfinancialyearflag.Value=1, Fields!Total_Delivered.Value, 0)) /
Sum(IIf(Fields!lastfinancialyearflag.Value=1, Fields!Total_Orders.Value, 0)))
this worked, with a combo of using your guys answers.. thank you team A *

SSRS Sum Values Based on Earliest Date

I'm trying to sum a net balance based on the earliest date in an SSRS report. In this case there are only 2 dates, but there can be more dates not more than 7 days.
Here's a sample of my data:
Here's what I'm trying to get with the earliest date of 10/26/15:
I've tried the following code, but not able to get this to work:
=Sum(IIf(DateDiff("d",Fields!SettleFullDate.Value,today())>=7
and DateDiff("d", Fields!SettleFullDate.Value, today())<7
and Fields!SETTLEBALANCE.Value>0), Fields!SETTLEBALANCE.Value, 0)
Update: I tried the code below and keep getting an error on the report. Could it be that I need to change the date field to an integer?
Thanks in advance for your help!
To compare the sum of values of two dates, the maximum and minimum in a set you can use the following equation
=Sum(iif(Fields!myDate.Value = Max(Fields!myDate.Value), Fields!myVal.Value, 0))
-Sum(iif(Fields!myDate.Value = MIN(Fields!myDate.Value), Fields!myVal.Value, 0))
This Sums all the values that match the maximum date in the dataset together, and sums all the values that match the minimum date in the dataset together, and takes one from the other.
It is irrespective of which dates you ask to be received, the above approach will work only against the records that you return to SSRS. So if you have a filter (WHERE clause) to return records between Date1 and Date2 this will still apply (Note - don't actually use 'Between' in the query)
Rather than using the maximum and minimum dates as listed here, you could also calculate a date similar to your original approach using
dateadd("d", -7, Fields!MySpecificDate.Value)
And insert that to the expression above.
Hopefully this is what you require - if not please let me know.

SUM of a SUM in SSRS 2008

I've this report
Here I make the first sum because I've grouped values from each month (months are "Gennaio", "Febbraio", "Marzo" etc etc.). These values are hidden, but anyway I get the sum and I display the sum for each month.
Then I should make the second sum that use values for each month and display the total for each category. Categories are "TOTALE LAVORI RESTAURO", "TOTALE LAVORI EDILE" etc.)
This is the final sum, where I sum values from each category.
Everything is working well, but now I have to add a "month" parameter to the report that returns sums until a selected month. This parameter changes the sum 1 with this expression:
=Sum(IIf(Fields!mese.Value <= Parameters!mese.Value, Fields!costi.Value, 0))
Now, how should I change expression in SUM2 and SUM3 to work with this parameter?
If I copy that code, ther returns #Error and as far as I know I can't use ReportItems sum.
So any suggestion?
SUM #1 could remain Sum(Fields!costi.Value) because you need to display every months.
i.e.: display GIUGNO even if Parameters!mese.Value = 4 (APRILE).
So you have only to change SUM #2 and #3 because TOTALE LAVORI RESTAURO and TOTALI must show only costi from GENNAIO to Parameters!mese.Value; i.e. if Parameters!mese.Value = 4 display only GENNAIO-APRILE even if we have details about GIUGNO.
The expression gave error because you have NULL value in Fields!costi.Value or Fields!mese.Value: convert this value to zero in your DataSet and you won't have problems.

MySQL: limit results by a calculated step interval

I have a need to return a specific number of rows from a query within a given start and stop time at a dynamically calculated step interval.
I've kept it simple here with a table consisting of a unix timestamp and a corresponding integer value.
In my example, I need to have 200 rows returned with an INCLUSIVE start time of 1307455099 and and an INCLUSIVE end time of 1307462455.
Here's the current query I've developed so far. It uses the modulus of the total rows to calculate the step interval:
SELECT timestamp, value FROM soh_data
WHERE timestamp % (CAST((1307462455 - 1307455099)/200 AS SIGNED INTEGER)) = 0
AND timestamp BETWEEN 1307455099 AND 1307462455
ORDER BY timestamp;
The first problem is that because I'm using a modulus, the start and end times aren't always inclusive (that's solvable with an extra query... I'm fine with that).
The second, and more difficult issue to tackle, is that the total rows returned in this case is only 196. In most queries, it's n-1.
FYI, this is on a MySQL database with millions of rows of data.
Any insights?
Since I'm fine with throwing away a few rows, but I'm not alright with too little data, I've come up with two different approaches.
First: I've decided to adapt my query to use FLOOR instead of CAST. In my example, the quotient of the division was 21.805. SQL rounded that up to 22. The right step interval for gathering more than 200 results was 21 (yielding 205 results). Using FLOOR will give me the step number of 21 I need. Unfortunately, I haven't fully tested this to ensure consistent results across larger sets:
SELECT DISTINCT timestamp FROM soh_data
WHERE timestamp % (FLOOR((1307459460 - 1307455099)/200)) = 0
AND timestamp BETWEEN 1307455099 AND 1307459460
ORDER BY timestamp;
The more reliable solution is to pre-calculate the step in code. This way, I can zero in on the step programmatically. In the following example, I use Ruby for readability, but my ultimate solution will be coded in C++:
lower = 1307455099
upper = 1307459460
limit = 200
range = lower..upper
matches = 0
stepFactor = ((upper-1) - (lower+1))/limit
while (matches <= (limit - 2)) do
matches = 0
range.each { |ts| matches += 1 if (ts % stepFactor == 0) }
stepFactor -= 1 # For the next attempt
puts "Step factor = #{stepFactor+1}"
puts "Matches = #{matches}"
end
The number of rows returned would depend entirely on how many timestamps match your condition, of course. Let's say your step value comes out to 2, so your modulo math boils down to 'only even numbered timestamps'. If by chance all items in your table have odd time stamps, then you're going to get 0 rows returned, even though there's (say) 500+ items within the time range.
If you need exactly 200, you'd probably be better off using LIMIT in some way.