Oracle的莫名奇妙错误ORA-01403等错误
2018-02-09 10:30
465 查看
1、case when 用于avg
需求:获得总体的平均值和最后三个月的平均值with temp as( select 100 d1,to_date('20170901','yyyyMMdd') d2 from dual union all select 100 d1,to_date('20171001','yyyyMMdd') d2 from dual union all select 100 d1,to_date('20171101','yyyyMMdd') d2 from dual union all select 100 d1,to_date('20171201','yyyyMMdd') d2 from dual union all select 100 d1,to_date('20180101','yyyyMMdd') d2 from dual union all select 100 d1,to_date('20180201','yyyyMMdd') d2 from dual ) select avg(d1) avg_d1, avg(case when d2>=to_date('20171201','yyyyMMdd') then d1 else 0 end) avg_error,--错误 avg(case when d2>=to_date('20171201','yyyyMMdd') then d1 else null end) avg_three--对的 from temp;
结果
d1 | avg_error | avg_three |
---|---|---|
100 | 50 | 100 |
2、!= 。not like 过滤掉空值
with temp as( select 100 4000 d1,'2018' d2 from dual union all select 100 d1,'2016' d2 from dual union all select 100 d1,'2017' d2 from dual union all select 100 d1,'2018' d2 from dual union all select 100 d1,null d2 from dual union all select 100 d1,null d2 from dual ) select d1, d2 from temp where d2!='2018';--运用了!= ,该处使用not like '2018'结果将一致
结果:null值不见了,是不是感觉到很疑惑
d1 | d2 |
---|---|
100 | 2016 |
100 | 2017 |
解决:
select d1, d2 from temp where nvl(d2,'0000')!='2018';--将null替换 --第二种 select d1, d2 from temp where d2!='2018' or d2 is null;
结果:
d1 | d2 |
---|---|
100 | 2016 |
100 | 2017 |
100 | |
100 |
3、select into 没有数据 ORA-01403
表table1数据如下:c1 | c2 |
---|---|
100 | 2016 |
100 | 2017 |
100 | 2015 |
100 | 2014 |
declare v_d1 varchar2(32); begin select c1 into v_d1 from where c2='8888';--没有任务记录 bdms_output.put_line(v_d1); end;
错误 ORA-01403:not found data/未找到任何数据
解决
加上聚合函数,好处:
1、多条记录返回时不会报错
2、为null值不会替换
select max(c1) into v_d1 from where c2='8888';--没有任务记录
解释:Oracle聚合函数底层可能在没有值时将数据替换为null。
为了保险起见,select into 最好加上聚合函数,或者在使用之前if判断记录数是否为1,即大于0。
相关文章推荐
- 在用toad执行oracle时发生的错误ORA-01403: 未找到数据
- Oracle常见错误:ORA-01403 的根本原因和解决方案
- Oracle Stream ORA-26786 ORA-01403 错误的解决
- Oracle Stream ORA-01403 错误的解决
- oracle错误编码ORA-010330解决方法
- oracle 远程连接提示 ora-12541错误
- Oracle ORA-4031错误产生的原因详解
- 【转】oracle因为日志错误无法打开 - ORA-00350
- myeclipse连接oracle出现ora-12705错误的解决办法
- oracle登录错误(ORA-01033:ORACLE initialization or shutdown in progress
- Oracle内部错误:ORA-00600[kfioTranslateIO03]一例
- 解决连接oracle 11g报ORA-01034和ORA-27101的错误
- ORACLE无法登录错误代码 ORA-01033的解决方法
- sqlplus登录oracle时ORA-01017: invalid username/password; logon denied的错误
- ora-01033:oracle initializationg or shutdown in progress 错误提示
- 连接Oracle 10g时ORA-12514: TNS: 监听进程不能解析在连接描述符中给出的 SERVICE_NAME 错误的解决
- oracle10 ora-12154错误的解决办法
- 解决Linux平台下Oracle tns本地测试报ORA-12514错误
- oracle sql 超长报ORA-01460错误
- Oracle常见错误——ORA-00845