Column Data Type - mySQL - 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.

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 :)

CountDistinct to return negative values gives #error

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)))

"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))

Mysql - Can you check for both a blank string and 0 in one condition?

I want to check in mysql if a column is either blank, ie '', or 0.
Is there a way to do this with one condition?
Like
WHERE order_id > ''
or
WHERE order_id != ''
Would either of these work, or is there a different solution?
This is more a question of data quality. In a well designed database, there should be a fairly clear-cut difference between '' and 0.
If you're being vague about it, there are quite a lot of values that could be interpreted as "blank" in addition to these two. NULL is the obvious one, but what about white space? Or a string containing 0.00, or even if you're looking for a numeric value, any non-numeric string.
Ideally, the data should be stored in a format that matches the type of data it is supposed to hold - for example, if you're expecting a numeric field, it should be an int, or another numeric type, depending on exactly what you want to store. That way, it can never contain a string value, so you would never need to check for it.
If you can't change the type in the DB itself, the next best solution is to cast the value as that data type you are expecting in the select query. eg:
SELECT CAST(myfield as int) as myfieldnum FROM table where myfieldnum != 0
See the MySQL manual for more info: http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html
However, in the end, it does depend on exactly what you are expecting the data field to contain, and how you want to react to different types of content.
Does this qualify as one condition?
... WHERE order_id IN ('0', '');
I experimented a bit and it seems the answer is:
WHERE order_id != 0
This will show results where order_id is not 0 and also not blank
why dont u use a smiple query where both of ur conditions are going to be tested
select * from tbl_name where order_id=' ' or order_id = 0
try this it will work