Null validation in Hive , looking for best approach - hive

I have condition in datastage which verifies 30 columns as not null and assigning value as '0' if anyone of the field is null like below,
isnull(columnA) or isNull(coloumnB) or isnull(columnC) or isNull(coloumnD) then 0 else 1.
we have option to use case statement in hive to set values, however we have to give use CASE statement per column like below,
select
case when (columnA is NULL) then 0
case when (columnB is NULL) then 0
case when (columnC is NULL) then 0
.
.
.
else 1
end as iValidColumn
From tablea
What i am looking?
Trying to look for option where we can validate all the 30 columns in condition for null validation.

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

Oracle query to fix the data

I have a column transaction_value , here is the requirement if the value is empty then i need to replace it with null
i have tried the below case statement
CASE WHEN nc.transaction_value WHEN IS NULL
THEN nc.transaction_value
ELSE nc.transaction_value
END nc.transaction_value

Conditional Where Statement

I am trying to setup a conditional statement that will determine which WHERE clause to use.
I need to check 2 separate conditions, one on each side of the AND and if results are greater than 1 then use a particular statement if not then use nothing
Like this logic but in pdw sql
WHERE
if cte1.counte > 1 then 'cte1.built is not null' else ''
AND
if cte2.countd > 1 then 'cte2.demo is not null' else ''
possible combinations:
WHERE CTE1.BUILD IS NOT NULL
WHERE CTE1.BUILD IS NOT NULL AND CTE2.DEMO IS NOT NULL
WHERE CTE2.DEMO IS NOT NULL
BLANK
Is this possible to do?
Thanks in advance
Something like this:
WHERE (cte1.counte > 1 and cte1.built is not null or cte1.counte <= 1) and
(cte2.countd > 1 and cte2.demo is not null or cte2.countd <= 1)

Clean up SQL Code

I have created a working SQL query but it's ugly. I am using the statement:
CASE WHEN p.Guests is null THEN 0 ELSE p.Guests
About 10 times in some calculations being done in the query and I am wondering if it is possible to do something like:
variable = CASE WHEN p.Guests is null THEN 0 ELSE p.Guests
and then instead of 123 * (CASE WHEN p.Guests is null THEN 0 ELSE p.Guests)
I could do 123 * variable. p.Guests is the number of guests attending an event and it varies based on each row in the query.
So my question is: is there a way to make a variable like this in SQL?
No need for a long-winded case statement when there's
ISNULL(p.Guests, 0)
http://msdn.microsoft.com/en-us/library/ms184325.aspx
You can use ISNULL:
select 123 * ISNULL(p.Guests,0) FROM ...
The ISNULL function will return the first parameter, unless it is null in which case it will return the second parameter.

Oracle SQL: Excluding a record from query results only when TWO conditions are met

So I have a pretty large Oracle SQL query. I want to add the following logic to my where clause, described in pseudocode below. Is it possible? I want to exclude a record if and only if BOTH columnA and columnB are null. If one or the other is null, that's okay.
IF (pfr.columnA && pfr.columnB != NULL)
exclude record
ELSE
do nothing
I tried the below except from my where clause, but obviously it does not accomplish what I need.
AND (pfr.columnA IS NOT NULL AND pfr.columnB IS NOT NULL)
where NOT (pfr.columnA is NULL and pfr.columnB is NULL);
WHERE
case when pfr.ColumnA is null then 0 else 1 end +
case when pfr.columnB is null then 0 else 1 end > 0
or
where (pfr.columnA is not null or pfr.columnb is not null)