CountDistinct to return negative values gives #error - reporting-services

I'm writing a simple expression to count distinct values in my sql query. For a subset of properties, if they equal "lost/sold" I want them to be negative, but I am receiving #Error. Any idea why this is?
=Countdistinct(IIF(Fields!PortfolioChangeStatusCodeID.Value="Lost/Sold",Fields!LeaseNumber.Value*-1,Fields!LeaseNumber.Value))

Fields!LeaseNumber.Value is either null or not numeric. Checking for both would solve this.
Clearly, there is no need to check for both if (for instance) the value could never be null.
=Countdistinct( IIF(Fields!PortfolioChangeStatusCodeID.Value="Lost/Sold",ISNOTHING(CINT(Fields!LeaseNumber.Value),0)*-1,ISNOTHING(CINT(Fields!LeaseNumber.Value),0)))

Related

Expression in query treated as string

I am having a hell of a time trying to add two calculated fields in a query together. My first record has field1= 1, and field2= 5, and the field that is trying to add them as 15!
So it’s treating them as a string.
When I try to use the function of SUM() I get an error of some of the other fields not being used in expressions, which I don’t understand.
Subtracting the two fields works as does multiplication.
I am unable to change the format of either fields in the properties as the drop down menu is blank.
Please help!
Aggregate functions act on rows not fields. Sum(field1) adds the values of field1 for group of records. Use aggregate functions in an aggregate (GROUP BY) query.
Plus (+) character will concatenate text values but add numeric. Apparently, your two fields are providing text values. Either correct the field data type or use function to convert values to number. Convert at least one field and Access will perform arithmetic instead of concatenation on all terms of expression.
Val(field1) + field2
This assumes no fields are Null. Number conversion functions will error with Null. Also, arithmetic with Null returns Null. If Null is possibility, handle with Nz().
Val(Nz(field1,0)) + Nz(field2,0)

PGSQL - No function matches the given name and argument types. You might need to add explicit type casts

This code gives an error. I have looked similar type questions and couldn't find the answer.
sum(COALESCE(((rpt.report_target_data::json->>'itemQuantity')::int)::int),0) as itemQuantity,
report_target_data is a json object and 'itemQuantity' is an element of that json. Sometimes that field contains an empty value. So when I try to get the sum it gives an error because postgres cannot get the sum if a column had a empty value. What is the wrong with the above code. It there a way to walk around that matter? Is there a way to calculate sum even if some rows contain empty values?
Here is the error of the above code ->
No function matches the given name and argument types. You might need to add explicit type casts.
In my case, it was not a COALESCE problem but I ended up in this question.
I noticed that my column values were characters (the varchar type) so what I did is:
select sum(cast(num_suf as int)) as total from results;
Just in case someone lands in this question again :)

SUM, SWITCH , OR SSRS

I have a two columns in SSRS. COL1 and COL2.
My goal is to calculate sum of totals in COL2 based on COL1 values
=SUM(Switch(
Fields!COL1.Value="ABC" OR
Fields!COL1.Value="qwe" OR
Fields!COL1.Value="xyz" OR
Fields!COL1.Value="ijk" OR
Fields!COL1.Value="lmn" OR
Fields!COL1.Value="tyu",Fileds!COL2.Value))
I am getting this error if I use the above code:
The Value expression for the textrun ‘Textbox78.Paragraphs[0].TextRuns[0]’ contains an error: [BC32017] Comma, ')', or a valid expression continuation expected.
How can I fix this?
I'm not sure what the issue is but there's no reason to use SWITCH if you only have one condition. This IIF should do the same thing:
=SUM(IIF(
Fields!COL1.Value="ABC" OR
Fields!COL1.Value="qwe" OR
Fields!COL1.Value="xyz" OR
Fields!COL1.Value="ijk" OR
Fields!COL1.Value="lmn" OR
Fields!COL1.Value="tyu", Fields!COL2.Value, 0))
You did spell fields wrong but I assume that was a typo. I haven't used Switch that much, maybe it requires at least two choices?

Column Data Type - mySQL

This is part of my query
(t.as_num_responded-sqrt(t.as_num_responded))/t.as_num_subjects as 'RF score'
where t is my database, the problem arrives when I try to use the coalesce function, it doesn't work on it, to replace the NULL to 0.
coalesce(t.as_num_responded-sqrt(t.as_num_responded))/t.as_num_subjects) as 'RF score'
Not sure if I'm using the coalesce wrong or if it is because the new column 'RF score' is created as a varchar, I need the values as floats with two decimals. Please, any help will be very helpful. Thank you.
My comment is pretty much an answer I guess.
Coalesce goes through a list of values and returns the first non-null value in doing so. Unfortunately you are giving it one arguement, and if that value is null...it goes to the next, finds nothing, and returns null.
2 ways of fixing...
coalesce (yourfunction,0)
where yourfunction is the (t.as_num_responded-sqrt(t.as_num_responded))/t.as_num_subjects) line
This will return 0 when your function is null.
Isnull(yourfunction,0)
Isnull is a more simple version...instead of taking a list of fields, it just does one field and replaces nulls with the second arguement of the isnull function.

"null" in mathematical calculations?

I'm trying to make a MySQL query that returns rows where (col_a+col_b-col_c+col_d) != col_e, where all of the columns are decimal and default to null. There is one row that I know of that meets these requirements, but when I ran the query with the above logic as the WHERE clause, the row didn't show up. I noticed that col_c was null, instead of a numerical value, and after changing it to 0 the row showed up when I ran the query.
Why did this happen? I have always assumed that null was interpreted as 0 in an instance such as the above?
NULL (as far as my interpretation goes) is unrepresentable data. The only appropriate tests for null are IS NULL, IS NOT NULL, and several functions made specifically to handle NULL values: http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html
You could say - IFNULL(col_c, 0)+col_d (COALESCE will work identically in this case).
More information on working with NULLs: http://dev.mysql.com/doc/refman/5.0/en/problems-with-null.html
Nate, NULL is NULL. MySQL is not going to do the type conversion automatically for you. A way around for that is change the table's column DEFAULT to 0. Or use a function IFNULL(col_c, 0) into your expression.
;-)
Anytime you do a mathematical calculation where one or more of the values might be NULL you need to account for it in the formula by using a numerical identity operation, since any calculation containing a NULL will have a result of NULL. Here are some common cases:
Addition or Subtraction use 0
ifnull(col1,0)+ifnull(col2,0)
ifnull(col1,0)-ifnull(col2,0)
Multiplication, Division, Exponents, or Modulus use 1
ifnull(col1,1)*ifnull(col2,1)
ifnull(col1,1)/ifnull(col2,1)
power(ifnull(col1,1),ifnull(col2,1))
mod(ifnull(col1,1),ifnull(col2,1))