ABinitio- how to get/return first not null column from a list of columns - ab-initio

I am new to ABinitio, need help :
I have 6 columns with address fields , trying to find function in Abinitio which returns first not null value taking list of columns as arguments(something similar to Coalesce in Teradata or NVL in Oracle)
my requirement : should return first non null column value from this (col1,col2,col3,col4..)

It's 3 month old, but in case somebody else looks for the same:
first_defined(col1, col2, col3, col4, col5, col6, default_value_if_you_need_it);

Related

Order by first non-null result that comes from two different columns

I want to browse through all values of two columns in a table:
if the value in column 1 is not null, select it, otherwise select the value in column 2 instead.
then sort the final result in alphabetical ascending order, wherever column its values came from.
I tried the following query but it doesn't work and I'm not even sure it is supposed to do what I want to do.
SELECT *
FROM table
ORDER BY (CASE WHEN col1 IS NOT NULL THEN 1 ELSE 2 END ),
col1 DESC,
col2 DESC)
Besides the fact that it doesn't work (nothing outputted), it seems to sort the values of each column separately while I want to sort the final set of values retrieved, regardless of the column they are from.
Thank you for your help.
If you want to fix it with the CASE expression, it'd look like the following:
SELECT *,
CASE WHEN col1 IS NOT NULL
THEN col1
ELSE col2
END AS col
FROM table
ORDER BY col
Although a nice option is using the COALESCE function. It returns the first non-null value in the list of arguments.
SELECT *, COALESCE(col1, col2) AS col
FROM table
ORDER BY col

SQL query (at least)

I'm trying to write an SQL query but I couldn't translate the idea into code, and I'm seeking help here.
So, what I want to perform is:
If one of these columns have value then 1 else 0, I mean, at least if one of these 4 columns have value then count.
I hope I'm clear enough, thank you in advance :)
for columns c1, c2, c3, c3 try
IF ( CONDITION, 0, 1)
where CONDITION is (A and B and C and D)
where A is ( ISNULL ( c1 ))
or combined
IF ( (ISNULL(c1) and ISNULL(c2) and ISNULL(c3) and ISNULL(c4)) , 0, 1)
Using conditional COUNT_IF and COALESCE():
SELECT COUNT_IF(COALESCE(col1, col2, col3, col4) IS NOT NULL) AS cnt
FROM tab;
If any column contains data then COALESCE will pick the first value, satisfy the IS NOT NULL condition and count it.

Generate MQsql query to get records

I am having the following table say "A"
"column1" "column2"
1 arafath#gmail.com
2 ram#gmail.com;arafath#gmail.com
3 tom#gmail.com
I want to get the records with the following condition.
Condition1:
If the column value exist in the any of the row, it will retrieve the matched rows
Condition2:
If the column value doesn't match with any of the row, it wants to retrieve all the rows
Eg: column2 = "ram#gmail.com"
Output should be "row 2"
Eg: column2 = "arafath#gmail.com"
Output should be "row 1, row 2"
Eg: column2 = "xxx#gmail.com" (Unmatched column)
Output should be all the rows (row 1, row 2, row 3)
Please help me out to solve the problem.
Thanks in advance.
Please try the below one.
SELECT col1, col2
FROM yourTable
where ( not exists (Select col2
FROM yourTable where col2 like 'xxx#gmail.com')
or col2 like 'xxx#gmail.com');
We can try using a union here:
SELECT col1, col2
FROM yourTable
WHERE col2 REGEXP '[[:<:]]ram#gmail.com[[:>:]]'
UNION ALL
SELECT col1, col2
FROM yourTable
WHERE col2 NOT REGEXP '[[:<:]]ram#gmail.com[[:>:]]' AND
NOT EXISTS (SELECT 1 FROM yourTable WHERE col2 REGEXP '[[:<:]]ram#gmail.com[[:>:]]');
Demo
The above strategy is that the first half of the union returns the matching record, if it exists. The second half of the union then returns all other records, but only if on match were found in the first half of the union. If a match were found, then the WHERE clause in the second half of the union would fail, and would return nothing.
Also, please note that storing comma separated (or semicolon separated) data in your MySQL tables is generally bad practice. I had to use REGEXP to get around this problem, but ideally if each email had a separate row, we would only need to use = equality.

sum two or more columns in mysql table but leave out the null field

How to sum two or three columns, but if any column is NULL then it should not affect the value of SUM.
As i get NULL if columns are added all together.
Here is the example of table i am trying to work on.
id col1 col2 col3 total
1 2 3 5
2 10 5 NULL
3 2 NULL NULL
This is the query i tried.
SELECT id,col1,col2,col3, (col1+col2+col3) AS Total FROM test_table;
This Query works perfectly fine if there is no NULL column, but if there is any null column then number added with null becomes null and i get null in result..
Below is the Result Screen Shot i added.
in above image i get NULL in Total column if any column in sum has null.
But results should be like 10,15,2 respected to there id's.
One way would be to use IFNULL function:
SELECT id,col1,col2,col3, (IFNULL(col1,0)+IFNULL(col2,0)+IFNULL(col3,0)) AS Total
FROM test_table;
Use coalesce to replace null values with 0. I prefer coalesce. In my experience it is database agnostic, where as isnull, ifnull, nvl and others are specific to the database.
SELECT col1,col2,col3, (coalesce(col1,0)+ coalesce(col2,0)+ coalesce(col3,0)) as total from test_table;
In this case you would need to use IsNull(Column, 0) to ensure it is always 0 at minimum.
SELECT col1, col2, col3, sum(isnull(col1,0) + isnull(col2,0) + isnull(col3,0)) AS Total FROM test_table group by id;
Modify your select statement to exclude nulls.

How to get the minimum of 3 values where some of them may be null in MySQL using LEAST()?

I found this question ( How to get the max of two values in MySQL? ) while looking for ways to compare discrete set of values.
I want to be able to get the minimum of few selected values where some of them may be null because they are optional but the MySQL docs says:
If any argument is NULL, the result is NULL.
Use a COALESCE() function on the nullable values:
select LEAST(COALESCE(#foo, <max int>), COALESCE(#bar, <max int>));
if you get the max int value, then both were null (unless there's a decent chance you could actually have the max int value as a real argument, then more logic is necessary)
How about this:
LEAST(COALESCE(col1, col2, col3),
COALESCE(col2, col3, col1),
COALESCE(col3, col1, col2))
Obviously this doesn't scale well to more than 3 values.
Works, is easily extendible, and doesn't rely on any values not being found in the data but probably heinously inefficient!
CREATE TABLE X
(
Id int primary key,
col1 int null,
col2 int null,
col3 int null
)
Query
SELECT id,
Min(CASE c
WHEN 1 THEN col1
WHEN 2 THEN col2
WHEN 3 THEN col3
END)
FROM x,
(SELECT 1 AS c
UNION ALL
SELECT 2
UNION ALL
SELECT 3) t
GROUP BY id
You have to hard code a big number say 99999.
LEAST( IFNULL(COL_1,999999) ,
IFNULL(COL_2,999999) ,
IFNULL(COL_3,999999) ,
IFNULL(COL_1,999999) )
i.e. just add IFNULL then the value or column name with a big enough integer.