开发者

MYSQL Bucketing issue with my query

开发者 https://www.devze.com 2023-03-24 07:24 出处:网络
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.

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 su开发者_如何学JAVAccess and the same error message.


The syntax is CASE...WHEN...THEN...[ELSE]...END. You're missing the ENDs.

Also, at the end of your query, it's just ELSE rate_amt, not ELSE THEN.

http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号