SSRS Divide by Zero error on totals with weighted average - reporting-services

When attempting to get a total weighted average interest rate I occasionally receive Error when there is only one item in certain columns. Having trouble with the Iif statement handling this:
=Iif(Sum(Fields!Current_Principal_Balance.Value) = 0, 0, SUM(Fields!Current_Principal_Balance.Value * Fields!WAIR.Value))/Iif(Sum(Fields!Current_Principal_Balance.Value) = 0, 1, SUM(Fields!Current_Principal_Balance.Value))

Moved your brackets slightly, this seems to work:
=Iif(Sum(Fields!Current_Principal_Balance.Value) = 0, 0,
SUM(Fields!Current_Principal_Balance.Value * Fields!WAIR.Value)/Iif(Sum(Fields!Current_Principal_Balance.Value) = 0, 1, SUM(Fields!Current_Principal_Balance.Value)))

Related

MS Access Case sensitive query giving incorrect result

Why do these queries give different results? Reference is a single character column and I would expect to have a result giving counts for upper and lower case letter 'r'.
Select SUM(IIF(StrComp([REFERENCE],'R',0) = 0, 1, 0)) AS BIG_R,
SUM(IIF(StrComp([REFERENCE],'r',0) = 0, 1, 0)) AS LITTLE_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
The result is that both BIG_R and LITTLE_R are the same and equal the count of BIG_R's
However,
Select SUM(IIF(StrComp([REFERENCE],'r',0) = 0, 1, 0)) AS LITTE_R,
SUM(IIF(StrComp([REFERENCE],'R',0) = 0, 1, 0)) AS BIG_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
Again LITTLE_R and BIG_R are the same, but this time they equal the count of LITTLE_R's
This looks like a bug in the way MS Access processes this type of query, or have I missed something here?
Access (or probably rather JetEngine) thinks that StrComp is called twice with the same argument and optimizes away one of the two calls.
A workaround is to compare the ASCII character values (Asc("r") = 114, Asc("R") = 82):
Select
SUM(IIF(Asc([REFERENCE]) = Asc('R'), 1, 0)) AS BIG_R,
SUM(IIF(Asc([REFERENCE]) = Asc('r'), 1, 0)) AS LITTLE_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
Yet another workaround:
Select SUM(IIF(StrComp([REFERENCE],Chr$(82),0) = 0, 1, 0)) AS BIG_R,
SUM(IIF(StrComp([REFERENCE],Chr$(114),0) = 0, 1, 0)) AS LITTLE_R
From [SYMREF]
Where [PROGRAM] = 'SOMEPROGRAM'
Here the two inputs to StrComp are clearly different. So, the second call not optimized away.

How to format to display leading zero

I am trying to display the duration in this format: 05:02:09 which is hour, minute and second.
At the moment I can display it without the leading zero in this format: 5:02:09
=IIF(
Fields!DataValue.Value < 0, 0,
Floor(Fields!DataValue.Value / 3600) &":"&Format(
DateAdd("s",IIF(Fields!DataValue.Value < 0, 0, Fields!DataValue.Value),"00:00"),
"mm:ss"
)
)
How can add a leading zero when the hour is less than 10?
I found a solution, which I tried before but failed. Strangely, it worked this time.
=IIF(
Fields!DataValue.Value < 0, 0,
Format(Floor(Fields!DataValue.Value / 3600), "00") &":"&Format(
DateAdd("s",IIF(Fields!DataValue.Value < 0, 0, Fields!DataValue.Value),"00:00"),
"mm:ss"
)
)

Adding an iif statement or where clause

Here is my current expression
=IIf(Fields!Units_Sold.Value = 0, 0, Fields!Total_Incidents.Value / IIf(Fields!Units_Sold.Value = 0, 1, Fields!Units_Sold.Value))
I need to add where Fields!.TECHNOLOGY = "Wired" to the above statement I tried a couple different things but with no success.
Try this:
=IIf(Sum(iif(Fields!technology.Value = "wired",Fields!Units_Sold.Value,0)) = 0,
0,
sum(iif(Fields!technology.Value="wired",Fields!Total_Incidents.Value,0)) /
IIf(Sum(iif(Fields!technology.Value = "wired",
Fields!Units_Sold.Value,0)) = 0, 1,
Sum(iif(Fields!technology.Value = "wired",Fields!Units_Sold.Value,0))
)
)
It is not tested but should work. Let me know if this could help you

#Error in a greater than iif sum statement

I'm trying to write some code that will prevent a #Error.
I don't know where my issue is.
=Sum(IIF(Fields!CurrentNumDaysOD.Value >= 1 And Fields!CurrentNumDaysOD.Value <= 10,
Fields!CurrentBalance.Value, "0")) / Sum(IIF(Fields!CurrentNumDaysOD.Value > 0,
Fields!CurrentBalance.Value, "0"))
I have also attempted this:
=Sum(IIF(Fields!CurrentNumDaysOD.Value >= 1 And Fields!CurrentNumDaysOD.Value <= 10,
Fields!CurrentBalance.Value, Nothing)) / Sum(IIF(Fields!CurrentNumDaysOD.Value > 0,
Fields!CurrentBalance.Value, Nothing))
If the result of the second half of your statement (after the /) is 0, then you will be dividing by 0, which will cause an error.

mysql_fetch_fields returns different length then expected

I need to create table in other database based on select result types. Query results can map to actual columns in table or not f.e. Select 1, c from char_length_test.
How to get actual column size after select statement using mysql C api?
I have created such table:
CREATE TABLE char_length_test (c char(22))
And using mysql_real_query to execute this query
SELECT c from char_length_test
Right after that I execute mysql_fetch_fields to get length of c field and expect it to be 22 as in create table statement. Unfortunately length contains value of 66 (3 times more then I expect). Tried different sizes but result is the same, length is always 3 times bigger.
Also used gdb to see if there is any other data field containing expected value:
{
name = "c",
org_name = "c",
table = "char_length_test",
org_table = "char_length_test",
db = "database",
catalog = "def",
def = 0x0,
length = 66,
max_length = 0,
name_length = 1,
org_name_length = 1,
table_length = 16,
org_table_length = 16,
db_length = 6,
catalog_length = 3,
def_length = 0,
flags = 0,
decimals = 0,
charsetnr = 33,
type = MYSQL_TYPE_STRING,
extension = 0x0
}
mysql_fetch_fields() and mysql_field_len() will return a number of bytes required to store a VARCHAR value, not the number of characters. For UTF-8 columns, this will return 3 times the actual column size, even though the documentation says otherwise. This is so that your C code will know how much memory to allocate. If you set your MySQL connection (not the table structure!) to a different character set, you will get different results.
Edit:
You can change the character set of a MySQL connection by calling mysql_set_character_set(). If you use an 8bit character set, you should get a number of bytes that matches the width of the database column, e.g.:
mysql_set_character_set(&mysql,'latin1');