SELECT statement with * and ( + ) - sql

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.

Related

SQL Select Query with a clause NOT CONTAINS

I want to make a query witch select only field doesn't contains the percent symbol "%".
this field takes values like 1, 1,5, 1,5%. I want select only 1 and 1,5 in this example.
my query is :
SELECT * FROM CommissionContract
WHERE commission DOESN'T CONTAINS THE SYMBOL "%"
Use the operator NOT LIKE to exclude the values that contain the char '%'
SELECT * FROM CommissionContract
WHERE commission NOT LIKE '%[%]%'
See the demo.
try with not like
SELECT * FROM CommissionContract
WHERE commission NOT LIKE '%[%]%'

SQL SUM to ignore NULL value

I have a table TEST_TABLE as follows:
Name x_col y_col
=======================
Jay NULL 2
This is a simplistic representation of a much larger issue but will suffice.
When I do the following query I get NULL returned
SELECT SUM(x_col + y_col) FROM TEST_TABLE WHERE Name='Jay'
I want it to be 2. I thought the SUM() method ignores NULL values. How can I ignore values that are null in this query? Or actually in general, as this is a problem for a lot of my algorithms.
You get NULL because NULL + 2 returns NULL. The SUM() has only one row, and if the + expression is NULL, then the SUM() returns NULL.
If you want NULL to be treated as 0, the use COALESCE():
SELECT SUM(COALESCE(x_col, 0) + COALESCE(y_col, 0))
FROM TEST_TABLE
WHERE Name = 'Jay';
One final note. If you start with your data and filtered out all rows, then the result will still be NULL. To get 0, you need an additional COALESCE():
SELECT COALESCE(SUM(COALESCE(x_col, 0) + COALESCE(y_col, 0)), 0)
FROM TEST_TABLE
WHERE Name = 'Jayden';
Use COALESCE to replace NULL with 0.
SELECT sum(coalesce(x_col, 0) + coalesce(y_col, 0)) FROM TEST_TABLE WHERE Name='Jay'

db2 query returning 0 records

I am trying to execute below query:
select * from trgdms.src_stm where trim(src_stm_code) in (select concat (concat('''', trim(replace(processed_src_sys_cd,',',''','''))),'''' ) from trgdms.batch_run_log where src_stm_cd_or_hub_cd='CTR' and file_process_dt='2015-06-19' and batch_id=42);
the src_stm_code column of table trgdms.src_stm have values like T19,T68,T73 etc. When I run the inner query alone i do get the correct result:
select concat (concat('''', trim(replace(processed_src_sys_cd,',',''','''))),'''' ) from trgdms.batch_run_log where src_stm_cd_or_hub_cd='CTR' and file_process_dt='2015-06-19' and batch_id=42
Result: 'T19','T68','T73'
Wondered if anyone has used something similar in db2?
Remove the concat part from the inner query. When you concatenate values, the list of values generated will be treated as a string and the query doesn't return any results.
select * from trgdms.src_stm where trim(src_stm_code)
in (select trim(replace(processed_src_sys_cd,',',''',''')) from trgdms.batch_run_log
where src_stm_cd_or_hub_cd='CTR' and file_process_dt='2015-06-19' and batch_id=42);

IFNull in MySQL

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

How to get the byte size of resultset in an SQL query?

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