ORACLE-树状数据结构获取各层级节点信息
平时工作中出报表时,要求分别列出员工的一级部门,二级部门....,在数据库中,部门表(unit)的设计一般为在表中维护每个部门的上级部门(pid字段),或者通过一个关联表(unit_link)维护层级关系(uid:部门表中的主键,pid:上级部门的id,这种可以解决一对多的关系)。
之前取各级部门时用到了很多子查询很麻烦,现在考虑了一个做法,用到了connect by,sys_connect_by_path(9i以后)。
sql语句如下,可以创建视图使用,只需改变临时表,可获得各节点的信息:
--开始--
/*with tmp as
(select b.objno,
b.objname,
sys_connect_by_path(b.objno, '>') objnos,
sys_connect_by_path(b.objname, '>') objnames
from budgetdept b
start with b.id in ('30461F609D0AB57BE050007F01004203',
'30461F609D09B57BE050007F01004203')
connect by prior b.id = b.pid)*/
with tmp as
(select u.id,u.objname,
sys_connect_by_path(u.id, '>') objnos,
sys_connect_by_path(u.objname, '>') objnames
from orgunit u, orgunitlink k
where u.isdelete = 0
and u.id = k.oid
start with u.id = '402881e70ad1d990010ad1e5ec930008'
connect by prior k.oid = k.pid)
select substr(t.objnos,
instr(t.objnos, '>', 1, 1) + 1,
case
when instr(t.objnos, '>', 1, 2) > 0 then
instr(t.objnos, '>', 1, 2) - instr(t.objnos, '>', 1, 1) - 1
else
length(t.objnos) - instr(t.objnos, '>', 1, 1)
end) objno1,
substr(t.objnames,
instr(t.objnames, '>', 1, 1) + 1,
case
when instr(t.objnames, '>', 1, 2) > 0 then
instr(t.objnames, '>', 1, 2) - instr(t.objnames, '>', 1, 1) - 1
else
length(t.objnames) - instr(t.objnames, '>', 1, 1)
end) objname1,
substr(t.objnos,
case
when instr(t.objnos, '>', 1, 2) > 0 then
instr(t.objnos, '>', 1, 2) + 1
else
999
end,
case
when instr(t.objnos, '>', 1, 3) > 0 then
instr(t.objnos, '>', 1, 3) - instr(t.objnos, '>', 1, 2) - 1
else
length(t.objnos) - instr(t.objnos, '>', 1, 2)
end) objno2,
substr(t.objnames,
case
when instr(t.objnames, '>', 1, 2) > 0 then
instr(t.objnames, '>', 1, 2) + 1
else
999
end,
case
when instr(t.objnames, '>', 1, 3) > 0 then
instr(t.objnames, '>', 1, 3) - instr(t.objnames, '>', 1, 2) - 1
else
length(t.objnames) - instr(t.objnames, '>', 1, 2)
end) objname2,
substr(t.objnos,
case
when instr(t.objnos, '>', 1, 4) > 0 then
instr(t.objnos, '>', 1, 4) + 1
else
999
end,
case
when instr(t.objnos, '>', 1, 4) > 0 then
instr(t.objnos, '>', 1, 4) - instr(t.objnos, '>', 1, 3) - 1
else
length(t.objnos) - instr(t.objnos, '>', 1, 3)
end) objno3,
substr(t.objnames,
case
when instr(t.objnames, '>', 1, 4) > 0 then
instr(t.objnames, '>', 1, 4) + 1
else
999
end,
case
when instr(t.objnames, '>', 1, 4) > 0 then
instr(t.objnames, '>', 1, 4) - instr(t.objnames, '>', 1, 3) - 1
else
length(t.objnames) - instr(t.objnames, '>', 1, 3)
end) objname3,
substr(t.objnos,
case
when instr(t.objnos, '>', 1, 4) > 0 then
instr(t.objnos, '>', 1, 4) + 1
else
999
end,
case
when instr(t.objnos, '>', 1, 5) > 0 then
instr(t.objnos, '>', 1, 5) - instr(t.objnos, '>', 1, 4) - 1
else
length(t.objnos) - instr(t.objnos, '>', 1, 4)
end) objno4,
substr(t.objnames,
case
when instr(t.objnames, '>', 1, 4) > 0 then
instr(t.objnames, '>', 1, 4) + 1
else
999
end,
case
when instr(t.objnames, '>', 1, 5) > 0 then
instr(t.objnames, '>', 1, 5) - instr(t.objnames, '>', 1, 4) - 1
else
length(t.objnames) - instr(t.objnames, '>', 1, 4)
end) objname4,
substr(t.objnos,
case
when instr(t.objnos, '>', 1, 5) > 0 then
instr(t.objnos, '>', 1, 5) + 1
else
999
end,
case
when instr(t.objnos, '>', 1, 6) > 0 then
instr(t.objnos, '>', 1, 6) - instr(t.objnos, '>', 1, 5) - 1
else
length(t.objnos) - instr(t.objnos, '>', 1, 5)
end) objno5,
substr(t.objnames,
case
when instr(t.objnames, '>', 1, 5) > 0 then
instr(t.objnames, '>', 1, 5) + 1
else
999
end,
case
when instr(t.objnames, '>', 1, 6) > 0 then
instr(t.objnames, '>', 1, 6) - instr(t.objnames, '>', 1, 5) - 1
else
length(t.objnames) - instr(t.objnames, '>', 1, 5)
end) objname5,
t.*
from tmp t;
--结束--
思路是这样的:通过connect by与sys_connect_by_path获取到部门路径的字符串,通过分割符(>)去截取。
例如: objno objname objnos objnames
D001 凤凰网 >D001 >凤凰网
D002 信息部 >D001>D002 >凤凰网>信息部
D003 研发组 >D001>D002>D003 >凤凰网>信息部>研发组
根据instr(objnos,'>',1,X)获取分割符的位置(instr(objnos,'>',1,X)登录0时说明后面不包含此字符了,返回0,会出现截取不正确的字符串,赋值999,从第999个字符开始截取,一般就会截取空字符串了),使用substr去截取,从而获得各层级的数据。
结果:
一级编号 一级名称 二级编号 二级名称 三级编号 三级名称 四级编号 四级部门 五级编号 五级名称 当前编号 当前名称
D001 凤凰网 D001 凤凰网
D001 凤凰网 D002 信息部 D002 信息部
D001 凤凰网 D002 信息部 D003 研发组 D003 研发组
目前只想到了这种方法
ORACLE-树状数据结构获取各层级节点信息的更多相关文章
- 浅谈oracle树状结构层级查询之start with ....connect by prior、level及order by
浅谈oracle树状结构层级查询 oracle树状结构查询即层次递归查询,是sql语句经常用到的,在实际开发中组织结构实现及其层次化实现功能也是经常遇到的,虽然我是一个java程序开发者,我一直觉得只 ...
- 浅谈oracle树状结构层级查询测试数据
浅谈oracle树状结构层级查询 oracle树状结构查询即层次递归查询,是sql语句经常用到的,在实际开发中组织结构实现及其层次化实现功能也是经常遇到的,虽然我是一个java程序开发者,我一直觉得只 ...
- 小程序插件使用wx.createSelectorQuery()获取不到节点信息
发现小程序一个bug, 在小程序插件中使用wx.createSelectorQuery()获取不到节点信息,需要在后面加入in(this) 例如: const query = wx.createSel ...
- uni-app——小程序插件使用wx.createSelectorQuery()获取不到节点信息
发现小程序一个bug, 在小程序插件中使用wx.createSelectorQuery()获取不到节点信息,需要在后面加入in(this) 例如: const query = wx.createSel ...
- C# 递归构造树状数据结构(泛型),如何构造?如何查询?
十年河东,十年河西,莫欺少年穷. 学无止境,精益求精 难得有清闲的一上午,索性写篇博客. 首先,我们需要准备一张表,如下范例: create table TreeTable ( TreeId ) no ...
- oracle 树状查询
做树状查询的时候,oracle有自己的优势,一条sql语句就可以搞定,而mysql这种数据库就只能用递归了... 递归的项目实例: //递归取到栏目路径 public List getTreeList ...
- oracle 树状结构递归 PL/SQL输出控制 包括空格输出控制
树状结构 存储过程中通过递归构建,类似BBS回帖显示,代码共三段: 建表,插入数据,创建存储过程显示: 1.create table article(id number primary key,con ...
- 树 List Leaves 【用数组模拟了树状结构建树+搜索叶子节点+按照特殊规律输出每个叶子节点】
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...
- C#树状图 初始默认选中节点
效果图: <script type="text/javascript"> $(document).ready(function () { GetTree(); GetG ...
随机推荐
- Visual Studio 2012 使用SvcUtil在MVC4客户端引用WCF服务
已创建服务. 若要测试此服务,需要创建一个客户端,并将其用于调用该服务.可以使用下列语法,从命令行中使用 svcutil.exe 工具来进行此操作: svcutil.exe http://localh ...
- 有用好看的CSS+JS+table 导航
预览效果图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dis ...
- SexyProgressBar
https://github.com/sunalong/SexyProgressBar
- centos 6.3 搭建git/gitosis/gitweb
1. git的安装和配置 (1)使用yum源安装git yum install git (2)创建git用户并设置密码 #useradd --home /home/git git #passwd gi ...
- Tomcat中部署WEB项目的四种方法
对Tomcat部署web应用的方式总结,常见的有以下四种: 1.[使用控制台部署] 访问Http://localhost:8080,并通过Tomcat Manager登录,进入部署界面即可. 2.[利 ...
- Linux 混杂设备、外部中断和输入子系统
混杂设备也是一种字符设备,主设备号固定为10.相对于普通字符设备驱动,它不需要自己去生成设备文件. 1.声明使用的头文件 #include <linux/miscdevice.h> 2.定 ...
- sql查询行转列
昨天下午碰到一个需求,一个大约30万行的表,其中有很多重复行,在这些行中某些字段值是不重复的. 比如有ID,NAME,CONTRACT_id,SALES,PRODUCT等,除了PRODUCT字段,其余 ...
- iOS 10 推送必看(高阶1)
来源:徐不同 链接:http://www.jianshu.com/p/3d602a60ca4f iOS10 推送必看(基础篇) 虽然这篇文章比较长,也不好理解,但是还是建议大家收藏,以后用到的时候,可 ...
- C#中的多态现象
面向对象的特征封装.继承和多态.Polymorphism(多态性)来源于希腊单词,指“多种形态”.多态性的一个重要特征是方法的调用是在运行时确定而不是编译时.在.NET中用于实现多态性的关键词有vir ...
- H - The Falling Leaves
Description Each year, fall in the North Central region is accompanied by the brilliant colors of th ...