What's the equivalent of ELSEIF in Apache hive? - hive

I'm looking for a conditional structure in Apache Hive like the following Java code:
if (condition) value
elseif (condition) value
...
else value
Is there a way to do this in HiveQL?

You can use the case statement in HIVE
CASE Statement
The syntax for the case statement is:
CASE [ expression ]
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
WHEN conditionn THEN resultn
ELSE result
END
Here expression is optional. It is the value that you are comparing to the list of conditions. (ie: condition1, condition2, ... conditionn).
All the conditions must be of same datatype. Conditions are evaluated in the order listed. Once a condition is found to be true, the case statement will return the result and not evaluate the conditions any further.
All the results must be of same datatype. This is the value returned once a condition is found to be true.
IF no condition is found to be true, then the case statement will return the value in the ELSE clause. If the ELSE clause is omitted and no condition is found to be true, then the case statement will return NULL
Example:
CASE Fruit
WHEN 'APPLE' THEN 'The owner is APPLE'
WHEN 'ORANGE' THEN 'The owner is ORANGE'
ELSE 'It is another Fruit'
END
The other form of CASE is
CASE
WHEN Fruit = 'APPLE' THEN 'The owner is APPLE'
WHEN Fruit = 'ORANGE' THEN 'The owner is ORANGE'
ELSE 'It is another Fruit'
END

Related

missing right parenthesis when using if

I am trying to use simple IF query in sql developer (Oracle), but i get 'missing right parenthesis' Error. I want to write all ID from my table TEST_TABLE, and write yes if ID is 1234, else 'no'. What am i doing wrong?
select id, if(id = 1234, 'yes', 'no') from TEST_TABLE t
Can't use IF there (IF is for control flow in PLSQL code, not queries), use CASE WHEN instead:
select id, CASE WHEN id = 1234 THEN 'yes' ELSE 'no' END as idIs1234 from TEST_TABLE t
The basic form of a case when is:
CASE
WHEN test THEN truevalue
[WHEN othertest THEN othertruevalue]
[ELSE falsevalue ]
END
CASE must do one or more tests that each return a single value. If no case matches (all tests are false) and there is no ELSE clause, null is returned.
There is also a form that tests a single variable for equality against various values:
CASE id WHEN 1234 THEN 'yes1' WHEN 2345 THEN 'yes2' ELSE 'no' END

Oracle Case Statement Expression usage, and TRUE/FALSE usage

According to the documentation
for the Case Statement:
The CASE statement evaluates a single expression and compares it against several potential values
It appears that I can put expressions in the case statement that evaluate to a number, such as A + B, but I can't put an expression that evaluates to a boolean such as A = B. I am forced to put the expression in the when clause. I would expect Oracle to treat TRUE and FALSE as 1 and 0 and apply the same rules to them.
Why is this not valid?
Boolean Expressions Do Not Execute
-- boolean expression in case statement compared to 1 and 0
select case a = b
when 1 then
'EQUAL'
else
'NOT EQUAL'
end as _RESULT
from dual;
--boolean expression in case statement compared to TRUE and FALSE
select case a = b
when TRUE then
'EQUAL'
else
'NOT EQUAL'
end as _RESULT
from dual;
Numeric Expressions Do Execute
--numeric expression in case statement compared to the number 3
select case a + b
when 3 then
'THREE'
else
'NOT THREE'
end as _RESULT
from dual;
Boolean Expressions in the When Clause Do Execute
select case
when a = b then
'EQUAL'
else
'NOT EQUAL'
end as _RESULT
from dual;

SQL Case statement with 'or' and 'and'

Can I use a case statement as follows?
CASE
WHEN (condition1 = 1 or 2)
AND condition2 = 3
THEN result = 'Result'
ELSE
NULL
END
Conditions 1 and 2 will be looking for different values, just fyi.
If not, is there a better way to write this?
Thank you!
Would this work?
CASE WHEN condition1 in (1, 2) AND condition2 = 3
THEN 'Result'
ELSE NULL
END
AS result
Well it depends on the system you are using. For example if you use Oracle and PL/SQL you can check the statement here
http://www.techonthenet.com/oracle/functions/case.php
What DB are you using? And do you want the statement in SQL or in some other kind of code or stored procedure?
I'm not sure what you want to do with the statement. In a select statement, it would be:
SELECT (CASE WHEN (condition1 = 1 or 2) AND condition2 = 3
THEN 'Result'
END) as result
You don't need the else because NULL is returned by the statement automatically if none of the when conditions are met.
In a where, it would be:
WHERE (condition1 = 1 or 2) AND (condition2 = 3) AND (result = 'Result')
The else condition is equivalent to false.

SQL INNERJOIN with a switch case and if statement in select

Please help:
I have to create a column based on an expression from other columns,
but before that expression hits, there has to be a date check
if the date is before a specific date- it must do an expression, if the date is after said date, it must do another expression,
THEN: theres a unique column that has the digits 1-10 in it, each number represents a different expression.
the inner join and selecting the rows are fine, its just the switch and if expression that are beating me
basically the statement needs to look like this
select column1 if(date<neededDate)
{select case ExpressionColumn
when 1 //do stuff
when 2 // do stuff
else// do nothing
}
select column1 if(date>neededDate)
{select case ExpressionColumn
when 1 //do stuff
when 2 // do stuff
else// do nothing
}
i hope this made sense
You need two case statement nested within another case statement, it can be done like following
SELECT CASE WHEN date > neededDate THEN
CASE ExpressionColumn
WHEN 1 THEN '1'
WHEN 2 THEN '2'
ELSE 'null'
END
WHEN date < neededDate THEN
CASE ExpressionColumn
WHEN 1 THEN '1'
WHEN 2 THEN '2'
ELSE 'null'
END
ELSE 'null'
END
FROM YourTable;
you have your syntax wrong:
select case sign(datediff('s', date, neededDate)) -- precision: second
when 0 then -- case missing in your spec !
else
case ExpressionColumn
when 1 then -- case 1
when 2 then -- case 2
else -- whatever
end
end
from whatever
;
replace each comment with the appropriate expression over columns.
in your case a searched case expression might be more convenient:
select case
when (date < neededDate) then
-- note the difference: instead of 'case x when y then ...' you write 'case when b then ...'
case ExpressionColumn
when 1 then -- case 1
when 2 then -- case 2
else -- whatever
end
when (date > neededDate) then
case ExpressionColumn
when 1 then -- case 1
when 2 then -- case 2
else -- whatever
end
else -- this is missing from your spec!
end
from whatever
;
You need to check CASE..WHEN..THEN in Sql server
Simple CASE expression:
CASE input_expression
WHEN when_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
Searched CASE expression:
CASE
WHEN Boolean_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END

Why does this case statement not work?

Why does this case statement not work? My table is defined as below. I want the case to return true when M = "M" or false if it is not. Something does not seem to look right with this CASE statement but this is the simple case statement. But I thought that could be as many WHEN values as needed but in this cases there is only True or False and it does not appear that there could be anything different.
The input should be all of the M column in the row. I am expecting the output to be one row since I only have one row that has an M value in the column. I am not certain if the case statement should return false in this case.
CASE M = "M <-- is this is what the criteria is based on
I found that this is not the case however from the post below. When i meant that this did not look right I was referring two the fact that the WHEN portion of the code
WHEN true THEN something
just did not look like it normally does.
Code:
select CASE M = "M"
WHEN true THEN "PPP"
WHEN false THEN "False -- Look Again!"
ELSE "Not Found"
END
from Bat;
Image:
New Code:
select *,
CASE
WHEN M = 'M' THEN
Case
when O = 'O' then 'you found a double (MM)' <--Compiles but does not show result
End
WHEN M is null THEN 'False -- Look Again!'
END as 'What is this value'
from Bat;
That fixed the first problem but you can embed case statements N deep I would think so if i search again on O it should return a correct. But the first case returns only one record. So my second case would have to search in the record correct.
The normal way to write this is:
select (CASE WHEN M = 'M' THEN 'PPP'
WHEN M <> 'M' THEN 'False -- Look Again!'
ELSE 'Not Found'
END)
from Bat;
or:
select (CASE WHEN M is null then 'Not Found'
WHEN M = 'M' THEN 'PPP'
ELSE 'False -- Look Again!'
END)
from Bat;
If I had to guess, your version has unexpected behavior for NULL values, but it is hard to say without knowing what you expect or what it is doing.
Change it like this
select CASE
WHEN M = 'M' THEN 'PPP'
WHEN M is null THEN 'False -- Look Again!'
ELSE "Not Found"
END
from Bat;