[英]How to find less than by decode?
ORA-00907: missing right parenthesis
00907\. 00000 - "missing right parenthesis"
SELECT last_name
,salary
,DECODE(salary, salary < 6000, 0.36,
salary < 8000, 0.38,
salary < 10000, 0.4,
salary < 12000, 0.42,
salary < 14000, 0.44,
salary < 16000, 0.45) as "TAX RATE"
FROM employees;
不要让你的生活成为痛苦。 使用CASE
表达式而不是DECODE
。
select case when salary < 6000 then 0.36
when salary < 8000 then 0.38
...
when salary < 16000 then 0.45
else null
end as tax_rate
from employees
如果你想用DECODE
来做,你会使用SIGN
function (检查薪水是否低于一定数量),然后根据需要嵌套尽可能多的DECODE
。 这行得通,但维护起来是一场噩梦。
我没有你的表,所以我会尝试使用 Scott 的emp
来说明它:
SQL> select ename, sal,
2 decode(sign(sal - 1000), -1, 'less than 1000',
3 decode(sign(sal - 1500), -1, 'less than 1500',
4 decode(sign(sal - 2000), -1, 'less than 2000'))
5 ) rate
6 from emp;
ENAME SAL RATE
---------- ---------- --------------
SMITH 840 less than 1000
ALLEN 1600 less than 2000
WARD 1250 less than 1500
JONES 2975
MARTIN 1250 less than 1500
BLAKE 2850
CLARK 2450
SCOTT 3000
KING 5000
<snip>
如果将它与CASE
进行比较,差异将非常明显:
SQL> select ename, sal,
2 case when sal < 1000 then 'less than 1000'
3 when sal < 1500 then 'less than 1500'
4 when sal < 2000 then 'less than 2000'
5 end rate
6 from emp;
ENAME SAL RATE
---------- ---------- --------------
SMITH 840 less than 1000
ALLEN 1600 less than 2000
WARD 1250 less than 1500
JONES 2975
MARTIN 1250 less than 1500
<snip>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.