How can I use consecutive conditional clause in SQL? - sql

I was trying to work with CASE WHEN in Postgresql in order to evaluate something and then do another thing. However, I need not only to check two things but they must be checked in consecutive order.
Example, let's say I have 3 columns: col1, col2 and col3.
I want to check first if col1 is greater than 0. After checking this I want to check if col2 is greater than 0. If that's the case, I will create another column which will be the sum of all of them. However, I can't do this:
select case when col1>0 and col2>0 then col1+col2+col3 end as...
I need to do something like this:
select case when col1>0 then (case when col2>0 then col1+col2+col3) else NULL end as...
But that doesn't work. So, what can I do?

You were close. You can do:
select
case when col1 > 0 then case when col2 > 0 then col1 + col2 + col3 end
else NULL end as my_column1
You had missed the inner end.
By the way, a CASE expression evaluates to NULL when no when clause is matched. Therefore, else NULL is redundant.

Related

Two conditions failure in teradata case - NOT NULL AND NOT 0

Trying to return a column, giving 1, when column1 is NOT NULL and different than 0. So far managed to do this:
MAX(CASE WHEN column1 IS NOT NULL
THEN CASE WHEN column1 <> 0 THEN 1 ELSE 0 END
ELSE 0 END)
Getting this error:
SELECT Failed. 2620: The format or data contains a bad character.
It works quite ok with NOT NULL as a single condition, though.
I'm not sure why you need to nest anything, but it looks like you're missing the non-equality sign.
You might try,
CASE
WHEN column1 IS NOT NULL AND column1 <> 0
THEN 1
ELSE 0
END
Alternatively, this will produce the same result as an OR operator, where, but CASE executes the WHEN clauses in order.
CASE
WHEN column1 IS NOT NULL
THEN 1
WHEN column1 <> 0
THEN 1
ELSE 0
END
But in your description, it sounds like you wanted BOTH conditions to be true, so it doesn't make sense to nest or use multiple WHEN statements, because you can just connect them together with AND

counif(something) the column A is not empty in tableau

How to make an expression in tableau that if the field is not empty then count another field?
Something like count if(something) the column A is not empty.
It sounds like you want to count all rows. If so, use: COUNT(*) or COUNT(1).
Otherwise, use COALESCE() or CASE. If "empty" means NULL, then:
COUNT(COALESCE(a, b))
If "empty" means something else, then something like:
COUNT(CASE WHEN a <> '' THEN a ELSE b END)
You need to mention that condition in where clause columnA is not null or <>''
select count(something_column) from your_table where columnA <>'' or columnA is not null
The Tableau calculation is ifnull([First Field], [Second Field])

SQL - Select only one value from multiple fields

Assume the following
col1 col2 col3 col4
------+----------+---------+---------
abc | | |
Yes, col1-4 have a space in them!
I want to select the column that is not a space . On row 1 it's col1, but on row 20 it may be col3, row 55 it may be col2, and so on. I need to return just that column.
There will always be only one column with a stored value within this range of four columns, I just need the one that actually has information in it.
This will be part of a greater query for a report, so regardless of what column abc is in I need that to look the same in every result case. Meaning I can't have the results be col1 for one case and col2 for the other because the report won't recognize. The column needs to always be called the same.
Yes, I know it's better to store NULLS versus spaces and why use four columns when only one can have data, why not use one. I've complained enougth about that so don't rip me a new one about bad db design because I AGREE.
SELECT LTRIM(RTRIM(col1 + col2 + col3 + col4))
Here we go... Why not add bad code to bad design. You could technically add all of the columns together and then trim them for leading/trailing spaces. I don't recommend it for performance on large scale deployments. Heck, I don't recommend this for any production script but I've been here before... Gotta do what you can to get it done.
Why not use a CASE statement, like
CASE WHEN col1 <> ' ' THEN col1
WHEN col2 <> ' ' THEN col2
WHEN col3 <> ' ' THEN col3
WHEN col4 <> ' ' THEN col4
END
You can do this:
case
when col1 != '' then col1
when col2 != '' then col2
...
end

postgresql - exclude any result with nulls

I'm doing a bunch of sum queries: SELECT col1 + col2 + col3 + ...
Some of the values in some of the columns are null. I'm checking for them by doing
SELECT CASE WHEN col1 is not null and col2 is not null and ...
I'm wondering if there is a more concise syntax for accomplishing this task.
Well, since the sum of any number and null is null, you can do something like the following (with the obvious ... filled in):
select big_honking_sum
from(
select col1+col2+col3+...+coln as big_honking_sum
from some_table
)sum
where big_honking_sum is not null;
Use COALESCE function if you can accept that a NULL value will be treated as 0.
SELECT COALESCE(Col1,0)
+ COALESCE(Col2,0)
+ COALESCE(Col3,0)
AS TOTAL FROM table;
Perhaps you could consider using 0 as default value instead of NULL if applicable.
You can have that even simpler:
SELECT foo
FROM ...
WHERE (-1 IN (col1, col2, col3)) IS NOT NULL
The IN expression will return NULL if (and only if) there is at least one NULL value among the tested values (and no match). So the whole expression evaluates to TRUE if (and only if) there is no NULL.
Edit: I have to correct myself! A positive match would stop the evaluation and return TRUE, even if a NULL is among the values. So you need a value that is guaranteed not to be in the set. Like 0 where all values are > 0 or -1 where all values are positive or you cannot use this expression for the purpose.

How can I assign one field's value to another field based on a third field?

I have three fields and if column three equals a certain value, I want to get the value of column 2. Can I do this in a SELECT, and would I use an IF or CASE statement?
A CASE statement would be the easiest way to do this. The following SQL compares two columns. You can alter this to use WHEN Col2 = 'xxxx' THEN xxxx'.
SELECT Col1, Col2, CASE
WHEN Col2 = Col1 THEN 'True'
ELSE 'False'
END AS 'Columns Match'
FROM Table
Hope this leads you in the right direction
Select
Case
when columnthree = 'VALUE'
then column2
else 'do something'
end as [column name]
from
table name
Also you might want to do some conversion if the datatypes for both columns are different
You could use a CASE clause within a SELECT statement - IF won't work this way in SQLServer.
select Alpha, Beta, Gamma,
case
when Alpha > 2 * Beta then Gamma
when Beta = Gamma then Alpha + 3
when Gamma = 2 then 13
else 42
end as 'Omega'
from Alphabet
First match wins.
CASE returns a value, so it may be helpful in various circumstances:
delete ... where case when Id > Limit then 1 else 0 end = 1
update ... set ShoeSize = case when Wide = 1 then ShoeSize + 1 else ShoeSize end, ...