I'm attempting to bucket rates in a user defineable range. The input from the user is a simple set of instructions about the buckets, but I can't get the SQL right.
Here's what I have
SELECT r.*,CASE
WHEN rate_amt < 0.25 THEN (rate_amt + 0.5) as rate_amt
WHEN (rate_amt >= 0.25 AND rate_amt < 0.5) THEN (rate_amt + 0.25) as rate_amt
WHEN (rate_amt >=0.5 AND rate_amt < 0.75) THEN (rate_amt + 0.01) as rate_amt
WHEN ELSE THEN (rate_amt) as rate_amt FROM (sc_module_rates r)
Running this SQL produces the error
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as rate_amt
WHEN (rate_amt >= 0.25 AND rate_amt < 0.5) THEN (0.25) as rate_amt
W' at line 2
The table looks like this:
+-------------------+
| rate | rate_amt |
---------------------
| fiji | 0.04 |
| somoa | 0.76 |
| japan | 0.51 |
| china | 0.25 |
I need my output to basically add the user defineable margin to these rates (a number, as shown in the query)
Can anyone tell me what I am doing wrong? I've tried changing the WHEN clause several times, for example:
AND(rate_amt >=0.5 , rate_amt < 0.75)
(rate_amt >=0.5 <0.75)
(rate_amt BETWEEN 0.5 and 0.75)
All with no success and the same error message.
Additionally, and it might not have been clear, there is only one "AS ResultColumn" only at the very END of your CASE/WHEN/END clause
SELECT r.*,
rate_amt + CASE WHEN rate_amt < 0.25 THEN 0.5
WHEN rate_amt >= 0.25 AND rate_amt < 0.5 THEN 0.25
WHEN rate_amt >=0.5 AND rate_amt < 0.75 THEN 0.01
ELSE 0.00
END as rate_amt