当前位置:数据库 > Oracle >>

oracle 空值处理

有一张表A,有两个字段 地区编码,地区名称 1 北美 11 加拿大 12 美国 2 东亚 21 中国 211东北 212西北 2111 黑龙江 现在我要得到这样的一张表: 北美 | 美国 | ‘---’| ‘--’ 北美 | 加拿大 | ‘--- |‘--’ 东亚 | 中国 | 东北 | ‘--’ 东亚 | 中国 | 西北 | ‘--’ 东亚 | 中国 | 东北 | 黑龙江 谢谢啊~
补充:这个是我做的,只能找出 包含值最多的这一项。
请求大家帮我看看,我这个sql怎么改就对了。
如果有更好的处理方法,也请说去思路, 谢谢啦
select b.aname,c.aname,d.aname from
(select ab.areaclcode acode, ab.areaclname aname
  from bq_th_bd_areacl ab
 where (select length(ab.areaclcode) from dual) = 1) a,
 
(select ab.areaclcode acode, ab.areaclname aname
  from bq_th_bd_areacl ab
 where (select length(ab.areaclcode) from dual) = 2) b,

(select ab.areaclcode acode, ab.areaclname aname
  from bq_th_bd_areacl ab
 where (select length(ab.areaclcode) from dual) = 4) c,
 
 (select ab.areaclcode acode, ab.areaclname aname
  from bq_th_bd_areacl ab
 where (select length(ab.areaclcode) from dual) = 6) d
 
 where a.acode = substr(b.acode,0,1) and b.acode = substr(c.acode,0,2) and c.acode = substr(d.acode,0,4)
追问:东亚 |  中国 |  东北 | ‘--’   这个是多余的,
我用‘|’是来分割字段的,
只能显示两列,后两列显示不出来。
我现在在试着改呢。

出来那种效果了, 非常感谢您的答案~select t.invclasscode , t1.invclasscode ,t2.invclasscode  from bd_invcl t  inner join bd_invcl t1  on length(t.invclasscode) = 1 and length(t1.invclasscode) = 3  and instr(t1.invclasscode, t.invclasscode) = 1 left join bd_invcl t2 on length(t2.invclasscode) = 4
and instr(t2.invclasscode, t1.invclasscode) = 1  如果只有一级节点,这条记录就差不出来.这个该怎么改呢。麻烦您帮我看一下啊,谢谢
答案:只有四级么?如果只有四级,可以写死,用同一个表重命名四次做连接,四个表分别取其中一级。但是,你这个东亚 |  中国 |  东北 | ‘--’是不是多了?因为可以关联到“东亚 |  中国 |  东北 |  黑龙江”这条记录,那么东北后边没有的那个,应该不需要吧?

思路就是同一个表四次使用,但是四次分别对应着的是四个子表,一级,二级,三级,四级。
四级是根据areacode这个字段的长度来定的。
然后四张表,前两张做直连,在第二张表的areacode定位第一张表的areacode,得到的值是1,即第二级的编码是以第一级的编码开头的而且长度是2。第三级和第二级做左连接,有数据就连接,没有数据会拼上'--'两个横线。同样编码规则是按照三级以二级开头且长度等于3。四级同样。
这样得到的结果,应该是你要的数据。但是对于"东亚 |  中国 |  东北 | ‘--’"这一行,是无法得到的,因为往下一级,东北是有数据的,所以可以得到。
select t.areaclname ||'|'|| t1.areaclname ||'|'|| nvl(t2.areaclname ,'--')||'|'||  nvl(t3.areaclname ,'--')
from   bq_th_bd_areacl  t inner join bq_th_bd_areacl  t1 on  length(t.areaclcode) = 1 and length(t1.areaclcode) = 2 and instr(t1.areaclcode, t.areaclcode) = 1
                 left join bq_th_bd_areacl  t2 on  length(t2.areaclcode) = 3 and instr(t2.areaclcode, t1.areaclcode) = 1
                 left join bq_th_bd_areacl  t3 on  length(t3.areaclcode) = 4 and instr(t3.areaclcode, t2.areaclcode) = 1

上一个:现在正在学oracle存储过程,介绍本有关的好书吧。
下一个:在oracle中, “ 查询各学员的总成绩,要求筛选出总成绩在140分以上的”怎么搞?

Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,