Unable to write case statement in Spark SQL - sql

I have written below query in Spark SQL using spark-shell and I am getting below error message
spark.sql(""" select case when Treatment == 'Yes' then 1 else 0 end AS 'All-Yes' from person """)
Error message-
org.apache.spark.sql.catalyst.parser.ParseException:
mismatched input ''All-Yes'' expecting <EOF>(line 1, pos 58).
Can someone please help me in this

The alias should be enclosed with backquotes
select case when Treatment == 'Yes' then 1 else 0 end AS `All-Yes` from person
though in general you shouldn't use non-standard, an incompatible names.

Related

Databricks "extraneous input expecting EOF" error

This took me a while to figure out so I thought I'd share to save someone else the pain. This is obviously dummy code to illustrate the issue.
This doesn't work:
%sql
Select 'A' as A -- I won't need this
, '1' as B;
Select 'Magic';
Error message:
Error in SQL statement: ParseException:
extraneous input 'Select' expecting {<EOF>, ';'}(line 4, pos 0)
== SQL ==
Select 'A' as A -- I won't need this
, '1' as B;
Select 'Magic';
^^^
This does work:
%sql
Select 'A' as A -- I wont need this
, '1' as B;
Select 'Magic';
And the difference in the single-quote in the comment on line 3.
When you use single-quote in the comment, you need to pass the comment in double quotes.
Example: -- I won't need this it should be -- "I won't need this"

Hive CASE not working in filter condition

I'm facing an issue with a filter condition in Hive with CASE WHEN clause. Below are the statement and error:
Statement:
select day
from db.tableName
where case when length(day) <> '19' then substr(day,1,19) else day end
Error:
org.apache.spark.sql.AnalysisException: filter expression 'CASE WHEN (NOT length(tableName.day) = '19') THEN substring(tableName.day, 1, 19) ELSE tableName.day END' of type string is not a boolean.;;
If I put case clause in select clause, the statement runs perfectly but put in where condition creates the error mentioned. Although the error I captured is from Zeppelin but the same error of string type isn't boolean is on Beeline as well.
Any pointers on the issue?
Your syntax is wrong. You don't have to use WHERE clause here.
Try following :
select case
when length(day) <> '19' then substr(day,1,19)
else day
end
from db.tableName;

SPARK SQL CASE WHEN > 0

I have been struggling with this for about 3 hours.
Running Spark 1.6
Trying to get this to work in the spark SQl context. evt_acct_app_id is an integer, why is this not working, in sql this is easy. I tried multiple variations of this, remove apostrophe and etc.
CASE evt_acct_app_id
WHEN evt_acct_app_id > '0' THEN '001'
ELSE '002'
END
AS EVNT_SUBTYPE_CD,
Keep getting this error:Got this unknown exception:
org.apache.spark.sql.AnalysisException: cannot resolve 'CASE evt_acct_app_id WHEN (cast(evt_acct_app_id as double) > cast(0 as double)) THEN 001 ELSE 002'
due to data type mismatch: key and WHEN expressions should all be same type or coercible to a common type;
Don't use single quotes if you are comparing to an integer:
(CASE WHEN evt_acct_app_id > 0 THEN '001'
ELSE '002'
END) as EVNT_SUBTYPE_CD,
In addition, when you have expressions for comparison, the correct syntax for CASE does not have a column name after the CASE.
Try below:
CASE
WHEN evt_acct_app_id > '0' THEN '001'
ELSE '002'
END
AS EVNT_SUBTYPE_CD,
Try this:
def evtChange(d:Column) = {when(d > 0,"001").otherwise("002")}
data.select( evtChange($"evt_acct_app_id").as("EVNT_SUBTYPE_CD") )

Searched Case works / Simple Case doesn't in Oracle

I have the following Searched Case field selection in a Oracle 10g SELECT query
(case
when LOADER_CELLS.CELL_MODE='RW' then 1
when LOADER_CELLS.CELL_MODE='R' then 2
end) as CELL_EDIT_MODE_ID
but if I write it as a Simple Case expression, as follows:
(case LOADER_CELLS.CELL_MODE
when 'RW' then 1
when 'R' then 2
end) as CELL_EDIT_MODE_ID
I get a ORA-12704: character set mismatch error on the when 'RW' line.
I gave a look to the Oracle documentation, and it seems my syntax is correct. http://docs.oracle.com/cd/B19306_01/server.102/b14200/expressions004.htm
Can someone help me on this?
" I supposed that it could be a encoding problem but I don't know how to "cast" the constant strings to a NVARCHAR"
you do it with "N" syntax.
case LOADER_CELLS.CELL_MODE
when n'RW' then 1
when n'R' then 2
end
eg
SQL> select case a when 'a' then 1 end from foo;
select case a when 'a' then 1 end from foo
*
ERROR at line 1:
ORA-12704: character set mismatch
SQL> select case a when n'a' then 1 end from foo;
CASEAWHENN'A'THEN1END
---------------------

Can somebody verify this for me in sql or teradata

I have a line of code in Oracle and I had to convert it into Teradata.
The Oracle query is
/* add to avoid invalid number due to junk in column */
AND regexp_instr(table.column, ''[^[:digit:]]'', 1, 1) = 0
The code I have written in Teradata
AND (CASE WHEN (POSITION('' '' IN TRIM(table.column)) > 0) OR (UPPER(TRIM(table.column))
(CASESPECIFIC) <> LOWER(TRIM(table.column)) (CASESPECIFIC))
THEN 1 ELSE 0 end ) = 0
The column is defined as a VARCHAR(20) but I only want to select rows where the data is all numeric. I cannot verify the Teradata query as it is a very long-running query and I don't have access to create tables or rather I can not verify the out put on the database I have. I some how tried and it looks like it works but I once wanted to verify the syntax and my understanding of REGEXP_INSTR.
If I am reading correctly and based on my testing this will break your logic (both return 1):
SELECT (CASE WHEN (POSITION('' '' IN TRIM('1234')) > 0) OR (UPPER(TRIM('1234'))
(CASESPECIFIC) <> LOWER(TRIM('1234')) (CASESPECIFIC))
THEN 1 ELSE 0 END )
SELECT (CASE WHEN (POSITION('' '' IN TRIM('abcd ef1')) > 0) OR (UPPER(TRIM('abcd ef1'))
(CASESPECIFIC) <> LOWER(TRIM('abcd ef1')) (CASESPECIFIC))
THEN 1 ELSE 0 END )
The Teradata Developer's Exchange contains a library of Oracle functions that have been converted to Teradata UDF's that may help you address this problem. With a little effort you could write your own UDF around the isdigit() C function. (isdigit)
It may not be helpful now, but one of the recently announced Teradata 14.0 features is support of regular expressions.
EDIT: Added TD 14 example with REGEXP_INSTR that should solve the problem
SELECT table.column
FROM table
WHERE REGEXP_INSTR(table.column, '[^[digit]])') = 0;