CASE WHEN conditional in HiveQL - hadoop

I'm having trouble bringing a case then into HiveQL.
I have a column with data:
if column is NULL = FALSE
if the column is PEOPLE = TRUE. It returns all results as TRUE.
What's wrong with my role?
SELECT
id,
datetime,
CASE
WHEN tb_people !="" THEN 'TRUE'
ELSE 'FALSE'
END
FROM <BD>.<TABLE>

!="" is only checking for blank strings. "" and NULL are two different things. As such, what you're looking for is:
SELECT
id,
datetime,
CASE
WHEN tb_people IS NOT NULL THEN 'TRUE'
ELSE 'FALSE'
END
FROM <BD>.<TABLE>
I'd also recommend using the boolean true and false rather than string literals. This would let you replace your entire case statement with ifnotnull(tb_people) https://hive.apache.org/javadocs/r3.0.0/api/org/apache/hadoop/hive/ql/exec/vector/expressions/IsNotNull.html.

Related

Oracle APEX: Multiple Conditions?

I wish to have a region displayed only if
SELECT * FROM REI_LABOUR_RATE_REQUEST
WHERE BILLING_PARTNER = :P6_FILTER_DEALER
returns at least one row and the element :P6_FILTER_YEAR is not NULL.
I tried this by writing:
SELECT * FROM REI_LABOUR_RATE_REQUEST
WHERE BILLING_PARTNER = :P6_FILTER_DEALER
AND :P6_FILTER_YEAR != NULL;
but that somehow never returns any row.
Did you try having a conditional display on the region with the type being "Value of item IS NOT NULL" and P6_FILTER_DEALER as the expression.
This should work as long as the value of the item is submitted and the item has the value stored in the session.
Region Source:
SELECT * FROM REI_LABOUR_RATE_REQUEST
WHERE BILLING_PARTNER = :P6_FILTER_DEALER
As #Tony Andrews says:
:P6_FILTER_YEAR != NULL will never be true. use :P6_FILTER_YEAR IS NOT
NULL instead. In fact the condition is redundant here, because
BILLING_PARTNER = :P6_FILTER_DEALER will only be true when
:P6_FILTER_YEAR is not null anyway.

If include? == false

I have an array and a string:
$header = ["Date", "Time", "Site Name", "Computer Name"]
columnName = "esfjk sdhf sdf"
and I am checking if $header contains columnName:
return if $header.include? columnName == false
The condition above always returns true, and the code carries on, even though the array doesn't contain the string.
I also have the same issue when $hash is a hash and recordNum is a number such as 99999999 which is not in it, and I do:
return if $hash.has_key? recordNum == false
Any reason for this happening?
Precedence.
$header.include? columnName == false
is interpreted as
$header.include?(columnName == false)
that is, usually,
$header.include?(false)
which is false. So what you want to do instead is this:
$header.include?(columnName) == false
But in your particular case, I would do this (thanks, Alex Wayne):
return unless $header.include?(columnName)
And if you do that, then you can go back to the parentheses-less form:
return unless $header.include? columnName

c# LINQ where with nullable boolean fields

I have the following query:
where !(tf.Shipped.HasValue == true || tf.Ordered.HasValue == true || tf.Processed.HasValue == true)
Note that Shipped, Ordered and Processed are all nullable Boolean fields.
What I am trying to do is to check that if Shipped or Ordered or Processed have a value of true, they should NOT be in the result.
In my case Ordered is true but I am still getting this records. Not sure what I am doing wrong.
You're checking whether the nullable bools have a value.
If that value is false, HasValue will still be true.
You probably want to write
where !(tf.Shipped == true || tf.Ordered == true || tf.Processed == true)
Comparing nullable bools is the only case where one should write == true.
However, you probably should not be using nullable bools in the first place.
Unless you have a meaningful distinction between null and false, you should use regular bools instead and save yourself a lot of headache.

HQL Query with multiple Criteria

I am trying to write a HQL Query which selectes rows from a table based on multiple criteria.
firstName,lastName
the catch is that the query should be flexible to ignore any empty or null values
so
select t from table t where (:firstname = '' or t.firstName = :firstName) AND
(:lastName = '' OR t.lastName = :lastName)
I would have thought this would work? But it doesnt - it never returns any rows? Any ideas what could be wrong here? I am very new to HQL thats why this question.
If I am understanding correctly you want a way to allow the user to search by firstName, lastName or both. So you should be checking if the parameter passed in is empty then don't make it a condition. If they supply all blank parameters it would return the whole table. Try:
select t from table t
where (:firstname IS NULL or t.firstName = :firstName) AND
(:lastName IS NULL OR t.lastName = :lastName)
(:firstname = '' or t.firstName = :firstName)
Your criteria is strange. If :firstname = '' and if a firstname (t.firstName) is equal '' in the database, the criteria t.firstName = :firstName is good ('' = '')
You don't need :firstname = ''
But If you want to check null value, you need to do:
t.firstName IS NULL or t.firstName = :firstname
What happens if you run following hql with firstname parameter set to empty string?
select t from table t where (:firstname = '')
and following with firstname parameter set to null:
select t from table t where (:firstname is null)
If any of the above returns the whole table then the HQLs named parameter might support what you are trying to do.
Otherwise you must use different queries for the null parameter cases. You can do this by generating the query dynamically.
I had a similar requirement. I want dynamic but I'm using a tool that just gives an HQL editor, so no Java.
The query below allows for optional parameters. Essentially a pseudo-quazi XOR of sorts . . . wish there was real XOR :/
With this query you just put NA into a param instead of leaving it empty if it is not needed.
Yeah, yeah, yeah . . . it's ugly, but it works and it's easy to alter to any other scenario needing optional params in pure HQL.
SELECT t AS table
FROM Table t
WHERE (t.valSet = :valSet
AND (:category= 'NA' AND :subCategory= 'NA'))
OR (:category != 'NA'
AND (t.valSet = :valSet
AND t.category= :category))
OR (:subCategory != 'NA'
AND (t.valSet = :valSet
AND t.subCategory = :subCategory ))

How Oracle 10g evaluates NULL in boolean expressions

if not (i_ReLaunch = 1 and (dt_enddate is not null))
How this epression will be evaluated in Oracle 10g
when the input value of the i_ReLaunch = null and the value of the dt_enddate is not null
it is entering the loop.
According to the rules in normal c# and all it should not enter the loop as
it will be as follows with the values.
If( not(false and (true))
= if not( false)
=if( true) which implies it should enters the loop
But it is not happening
Can someone let me know if i am wrong at any place
Boolean operations with NULL value in Oracle return UNKNOWN - not true or false. So you have something like this:
If( not(UNKNOWN and (true)) = if not( UNKNOWN) =if( UNKNOWN )
In this case, IF will treat UNKNOWN as false.
If i_relaunch can be null, then you need to use some of NULL handling functions(NVL, NVL2, NULLIF, COALESCE, LNNVL) to be sure that you have correct result.
See these article for more information:
Nulls: Nothing to Worry About
Fundamentals of PL/SQL. Scroll down to - Handling Null Values in Comparisons and Conditional Statements

Resources