I am new to SQL and I am trying to create a calculated field using a set of columns. However, some of those values may be NULL. If they are NULL, I don't want the calculated field to return a NULL result but instead set some arbitrary value.
Here is the calculated field
(ces.EXPERT_SCORE * cirm.CONSUMER_RATING) + (12.5 * scs.SIMILARITY)
use IFNULL(tocheckwhat,withwhattoreplace) so:
(IFNULL(ces.EXPERT_SCORE,5) * IFNULL(cirm.CONSUMER_RATING,5)) + (12.5 * IFNULL(scs.SIMILARITY,5))
should work! :)
You can use COALESCE operator
(
COALESCE(ces.EXPERT_SCORE, <YOUR_ARBIT_VALUE>) *
COALESCE(cirm.CONSUMER_RATING, <YOUR_ARBIT_VALUE>)
)
+ (12.5 * COALESCE(scs.SIMILARITY, <YOUR_ARBIT_VALUE>))
Related
What does this select statement mean with * and (column_name1 + column_name2)?
I don't understand it when they have * and ().
SELECT
ProductName,
UnitPrice * (UnitsInStock + COALESCE(UnitsOnOrder, 0))
FROM
Products;
Just for a question, didn't try anything.
Your SQL statement basically says that select two columns from Products which is ProductName and a calculated field, the UnitPrice * (UnitsInStock + COALESCE(UnitsOnOrder, 0)).
To simply put:
First, the COALESCE returns the non-NULL value. In this case, it will return 0 if the value of the "UnitsOnOrder" column is NULL, just like #Jonas Metzler says. Second, the "UnitsInStock" column value is added to the result of the COALESCE. Finally, the result from the second step is multiplied by "UnitPrice", which gives the total value of the product.
I have the following statement within my select clause;
(([Complt_Emp] + [No_Non_Complt_Emp])/ [No_of_Emp]) as Total_Completed
How do I implement CAST "cast(your_float_column as decimal(10,2))" ? I want my column Total_Completed to show 2 decimal places
I cannot seem to get the correct syntax!
Thank you
the result of the calculation depends of the used columns type.
If you divide int columns, you get int result : 1 / 6 = 0
when you convert each values to decimal the result is: 1 / 6 = 0.1666666666666
Now you want 2 decimal result,so you have to convert/ round the previous result to get the expected value
See fiddle for some example of divide and cast / round : http://sqlfiddle.com/#!18/51785/5
An easy trick can be to use :
round ( 1.0 * ( [Complt_Emp] + [No_Non_Complt_Emp] ) / [No_of_Emp] , 2 )
Cast each expression seperately
CAST(([Complt_Emp] + [No_Non_Complt_Emp]) as decimal(10,2)) /
CAST([No_of_Emp] as decimal(10,2)) as Total_Completed
I suspect all your values are INT. An int divided by an int will return an int.
(([Complt_Emp] + [No_Non_Complt_Emp])/ cast([No_of_Emp] as decimal(10,2)) as Total_Completed
Try this
cast((([Complt_Emp] + [No_Non_Complt_Emp])/ [No_of_Emp]) as decimal(10,2)) as Total_Completed
why does this work,
select*
from some_table
WHERE some_column_name like '%i%'
and not this?
select*
from some_table
WHERE
some_column_name like (select ''''+'%' +value +'%' + '''' as val
from [dbo].[fn_Split](' i this is a test testing Chinese undefined',' ')
where idx = 0)
I am trying to search for individual words instead of the whole phrase, the split function above will split the string on space characters and plug the results into a table with two columns, idx and value.
the LIKE operator takes a string for an argument. It cannot be used on a table, which I assume your function returns.
I think what you want to do is JOIN to the function, and then check where LIKE fn.Value:
select *
from some_table t
INNER JOIN (select value as val
from [dbo].[fn_Split](' i this is a test testing Chinese undefined',' ')
where idx = 0) f
ON t.some_column_name like '%'+f.val+'%'
If your subquery is guaranteed to only return one result, you could try putting the modulo symbols around it instead of inside it:
LIKE '%' + (YourSubQuery) + '%'
One possible reason is because you are appending single quotes onto the beginning and end of the string, and none of the values actually store single quotes in the string.
Another reason is might not work is because the subquery returns more than one row or zero rows. The function fn_split() is your own function, so I don't know what it returns. You have a subquery in a context where it can return at most one row and one column. That is called a scalar subquery. If the subquery returns more than one row, you will get an error. If the subquery returns no rows -- for instance, if idx starts counting at 1 rather than 0 -- then it will return NULL which fails the test.
If you want to find a match this way, I would recommend exists:
select t.*
from some_table t
where exists (select 1 as val
from [dbo].[fn_Split](' i this is a test testing Chinese undefined',' ') s
where s.idx = 0 and
t.some_column_name like '%' + value + '%'
);
The results of your sub-query is a literal string. The % symbol isn't seen as a wildcard. Also, does your functions return multiple rows? If so, LIKE operator can only evaluate a single value.
If your functions does return a single value, I would suggest looking into using Dynamic SQL. Something like the following:
DECLARE #SQL VARCHAR(MAX), #WildCard VARCHAR(MAX)
SELECT #WildCard = '%' + value + '%'
FROM [dbo].[fn_Split](' i this is a test testing Chinese undefined',' ')
WHERE idx = 0
SET #SQL = 'SELECT * FROM some_table WHERE some_column_name like ''' + #WildCardWildCard + ''''
EXEC(#SQL)
Is it possible to get the size in bytes of the results of an sql query in MySQL?
For example:
select * from sometable;
ths returns 10000 rows. I don't want the rows but the size of the resultset in bytes. Is it possible?
select sum(row_size)
from (
select
char_length(column1)+
char_length(column2)+
char_length(column3)+
char_length(column4) ... <-- repeat for all columns
as row_size
from your_table
) as tbl1;
char_length for enum, set might not accurate, please take note
To build on Angelin's solution, if your data contains nulls, you'll want to add IFNULL to each column:
select sum(
ifnull(char_length(column1), 0) +
ifnull(char_length(column2), 0) +
ifnull(char_length(column3), 0) +
ifnull(char_length(column4), 0) ... <-- repeat for all columns
)
from your_table
simplify :
select sum(char_length(column1)+
char_length(column2)+
char_length(column3)+
char_length(column4) ... )<-- repeat for all columns
from your_table
You need to add IFNULL() to each column as #futilerebel has mentioned
CHAR_LENGTH() gets number of characters if unicode will be more bytes - use LENGTH() for number of bytes:https://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_length
I have a column where I need to update it's value by random numbers between 1 and 3150 (just being specific)
Can I do this with a simple TSQL statement?
Use RAND(), re-seeding the function on each call.
UPDATE MyTable
SET MyColumn = 1 + FLOOR(3150 * RAND(CONVERT(varbinary, NEWID())))
WHERE ...