cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

I am new to Databricks and SQL. My CASE statement is not working and I cannot figure out why. Below is my code and the error message I'm receiving. Grateful for any and all suggestions. I'm trying to put yrs_to_mat into buckets.

am777
New Contributor
SELECT *, yrs_to_mat,
 CASE 
      WHEN < 3 THEN "under3"
      WHEN => 3 AND < 5 THEN "3to5"
      WHEN => 5 AND < 10 THEN "5to10"
      WHEN => 10 AND < 15 THEN "10to15"
      WHEN => 15 THEN "over15"
      ELSE null END AS maturity_bucket
FROM matyrs; 
Error in SQL statement: ParseException: 
[PARSE_SYNTAX_ERROR] Syntax error at or near 'THEN'(line 3, pos 15)
 
== SQL ==
SELECT *, yrs_to_mat,
 CASE 
      WHEN < 3 THEN "under3"
---------------^^^
      WHEN => 3 AND < 5 THEN "3to5"
      WHEN => 5 AND < 10 THEN "5to10"
      WHEN => 10 AND < 15 THEN "10to15"
      WHEN => 15 THEN "over15"
      ELSE null END AS maturity_bucket
FROM matyrs

1 REPLY 1

Pat
Honored Contributor III

Hi @Anne-Marie Wood​ ,

I think it's more SQL general issue:

you are not comparing any value to `< 3`

it should be something like :

WHEN X < 3 THEN "under3"

    SELECT *, yrs_to_mat,
     CASE 
          WHEN X < 3 THEN "under3"
          WHEN X => 3 AND < 5 THEN "3to5"
          WHEN X => 5 AND < 10 THEN "5to10"
          WHEN X => 10 AND < 15 THEN "10to15"
          WHEN X => 15 THEN "over15"
          ELSE null END AS maturity_bucket
    FROM matyrs; 

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!