0

I want to run a case statement that runs different SELECT Statements Based on a condition in Spark SQL but not able to get the Syntax Right .

My SQL statement looks like this

registerTable(sql="SELECT CASE WHEN typedKeyword > '' THEN (SELECT * FROM `temp.sdf0` WHERE  originalKeyword > ''AND  keyword > '' AND deviceType = 'devicetype' ) ELSE (SELECT * FROM `temp.tes` WHERE  originalKeyword > ''AND  keyword > '' ) END  ",alias="temp.test")

I don't know if CASE statement is supported in spark SQL so How can one achieve this

4
  • 1
    You can't put complete selects into a CASE Commented Sep 28, 2016 at 11:02
  • 2
    You should handle that in your logic and execute 2 different queries depending on the input Commented Sep 28, 2016 at 11:03
  • Thanks . Was Just interested if its possible Commented Sep 28, 2016 at 11:04
  • Or the best way for handling this is to create a Stored Proc as handling 2 different queries in not appreciated in SQL. Commented Sep 28, 2016 at 11:25

1 Answer 1

1

I've worked in nearly a dozen SQL and SQL-like dialects and I've never seen syntax like that. It looks like there's a core misunderstanding of what SQL clauses are supposed to do what.

The SELECT clause is for describing a projection of scalar elements. In many dialects you can issue subqueries - sometimes even correlated subqueries - in this clause, but you always must apply an operator that converts the result into a scalar value. For example:

SELECT (CASE WHEN EXISTS (SELECT foo FROM tbl) THEN 1 ELSE 0 END)

Here the "EXISTS" operator converts the uncorrelated subquery into a scalar boolean, so it's legal.

The lack of a FROM clause in the top level query is also a big red flag. It, or some analogue like Oracle's "dual", is legal in about half of the dialects I've seen, but if what you're trying to do is dynamically switch between two queries, you're almost certainly doing it wrong. juergen is right - what you probably meant to do is use 2 different queries.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.