我有一组数据,当3<R<=10时,R为A,当10<R<=20时R为B,当20<R<=30时R为c ,iv 30<R时,R为D,请问如何写SQL语句。
SELECT ( CASE WHEN 3<R<=10 THEN 'A' WHEN 10<R<=20 THEN 'B' WHEN 20<R<=30 THEN 'C' WHEN R>30 THEN 'D' ELSE '小于等于3的情况值' END ) AS R FROM TABLE
补充:
一楼SQL有个很明显的BUG~会把"小于等于3"和"大于30"的值全部赋值为D...题意是"当R>30时,才为D"
select (case when 3<R<=10 then 'A'
when 10<R<=20 then 'B'
when 20<R<=30 then 'C'
else 'D' end ) as R from table
SELECT CASE WHEN R>3 AND R<=10 THEN 'A' WHEN R>10 AND R<=20 THEN 'B' WHEN R>20 AND R<=30 THEN 'C' WHEN R>30 THEN 'D' ELSE '其他' END AS R FROM TABLE