【转】 oracle 层次查询判断叶子和根节点
Oracle 9i判断是叶子或根节点,是比较麻烦的一件事情,SQL演示脚本如下:
- DROP TABLE idb_hierarchical;
- create TABLE idb_hierarchical
- (
- id number,
- parent_id number,
- str varchar2(10)
- );
- insert into idb_hierarchical values(1,null,'A');
- insert into idb_hierarchical values(2,1,'B');
- insert into idb_hierarchical values(3,2,'C');
- insert into idb_hierarchical values(4,3,'D');
- insert into idb_hierarchical values(5,2,'E');
- insert into idb_hierarchical values(6,2,'F');
- insert into idb_hierarchical values(7,3,'G');
- insert into idb_hierarchical values(8,4,'H');
- insert into idb_hierarchical values(9,4,'I');
- insert into idb_hierarchical values(10,null,'J');
- insert into idb_hierarchical values(11,10,'K');
- insert into idb_hierarchical values(12,11,'L');
- insert into idb_hierarchical values(13,10,'M');
- DROP TABLE idb_hierarchical;
- create TABLE idb_hierarchical
- (
- id number,
- parent_id number,
- str varchar2(10)
- );
- insert into idb_hierarchical values(1,null,'A');
- insert into idb_hierarchical values(2,1,'B');
- insert into idb_hierarchical values(3,2,'C');
- insert into idb_hierarchical values(4,3,'D');
- insert into idb_hierarchical values(5,2,'E');
- insert into idb_hierarchical values(6,2,'F');
- insert into idb_hierarchical values(7,3,'G');
- insert into idb_hierarchical values(8,4,'H');
- insert into idb_hierarchical values(9,4,'I');
- insert into idb_hierarchical values(10,null,'J');
- insert into idb_hierarchical values(11,10,'K');
- insert into idb_hierarchical values(12,11,'L');
- insert into idb_hierarchical values(13,10,'M');
示例数据清单如下:
- SELECT RPAD('+',LEVEL*2+1,'.')||STR STR_LEVEL,ID,PARENT_ID,LEVEL LVL
- FROM idb_hierarchical
- START WITH PARENT_ID IS NULL
- CONNECT BY PARENT_ID = PRIOR ID;
- SELECT RPAD('+',LEVEL*2+1,'.')||STR STR_LEVEL,ID,PARENT_ID,LEVEL LVL
- FROM idb_hierarchical
- START WITH PARENT_ID IS NULL
- CONNECT BY PARENT_ID = PRIOR ID;
STR_LEVEL | ID | PARENT_ID | LVL |
---|---|---|---|
+..A | 1 | 1 | |
+….B | 2 | 1 | 2 |
+……C | 3 | 2 | 3 |
+……..D | 4 | 3 | 4 |
+……….H | 8 | 4 | 5 |
+……….I | 9 | 4 | 5 |
+……..G | 7 | 3 | 4 |
+……E | 5 | 2 | 3 |
+……F | 6 | 2 | 3 |
+..J | 10 | 1 | |
+….K | 11 | 10 | 2 |
+……L | 12 | 11 | 3 |
+….M | 13 | 10 | 2 |
在表1中,ID为8、9、 7、5、6、12、13都没有子节点,因此称为叶节点。
1.oracle9i 查询叶节点
只显示叶子节点SQL
- SELECT RPAD('+',LEVEL*2+1,'.')||STR STR_LEVEL,ID,PARENT_ID,LEVEL LVL
- FROM idb_hierarchical I
- --在oracle 9i中显示叶节点,需要判断是否有子节点即可
- WHERE NOT EXISTS(SELECT 1
- FROM idb_hierarchical B
- WHERE I.ID=B.PARENT_ID)
- START WITH PARENT_ID IS NULL
- CONNECT BY PARENT_ID = PRIOR ID;
- SELECT RPAD('+',LEVEL*2+1,'.')||STR STR_LEVEL,ID,PARENT_ID,LEVEL LVL
- FROM idb_hierarchical I
- --在oracle 9i中显示叶节点,需要判断是否有子节点即可
- WHERE NOT EXISTS(SELECT 1
- FROM idb_hierarchical B
- WHERE I.ID=B.PARENT_ID)
- START WITH PARENT_ID IS NULL
- CONNECT BY PARENT_ID = PRIOR ID;
STR_LEVEL | ID | PARENT_ID | LVL |
+……….H | 8 | 4 | 5 |
+……….I | 9 | 4 | 5 |
+……..G | 7 | 3 | 4 |
+……E | 5 | 2 | 3 |
+……F | 6 | 2 | 3 |
+……L | 12 | 11 | 3 |
+….M | 13 | 10 | 2 |
显示所有节点,标明该行是否为叶节点SQL
- SELECT RPAD('+',LEVEL*2+1,'.')||STR STR_LEVEL,ID,PARENT_ID,LEVEL LVL,
- NVL((SELECT 'N'
- FROM idb_hierarchical B
- WHERE I.ID=B.PARENT_ID
- AND ROWNUM < 2),'Y') IS_LEAF
- FROM idb_hierarchical I
- START WITH PARENT_ID IS NULL
- CONNECT BY PARENT_ID = PRIOR ID;
- SELECT RPAD('+',LEVEL*2+1,'.')||STR STR_LEVEL,ID,PARENT_ID,LEVEL LVL,
- NVL((SELECT 'N'
- FROM idb_hierarchical B
- WHERE I.ID=B.PARENT_ID
- AND ROWNUM < 2),'Y') IS_LEAF
- FROM idb_hierarchical I
- START WITH PARENT_ID IS NULL
- CONNECT BY PARENT_ID = PRIOR ID;
STR_LEVEL | ID | PARENT_ID | LVL | IS_LEAF |
+..A | 1 | 1 | N | |
+....B | 2 | 1 | 2 | N |
+......C | 3 | 2 | 3 | N |
+........D | 4 | 3 | 4 | N |
+..........H | 8 | 4 | 5 | Y |
+..........I | 9 | 4 | 5 | Y |
+........G | 7 | 3 | 4 | Y |
+......E | 5 | 2 | 3 | Y |
+......F | 6 | 2 | 3 | Y |
+..J | 10 | 1 | N | |
+....K | 11 | 10 | 2 | N |
+......L | 12 | 11 | 3 | Y |
+....M | 13 | 10 | 2 | Y |
oracle 9i 查询根节点
- SELECT RPAD('+',LEVEL*2+1,'.')||STR STR_LEVEL,ID,PARENT_ID,LEVEL LVL
- FROM idb_hierarchical I
- START WITH id =2
- CONNECT BY PARENT_ID = PRIOR ID;
- SELECT RPAD('+',LEVEL*2+1,'.')||STR STR_LEVEL,ID,PARENT_ID,LEVEL LVL
- FROM idb_hierarchical I
- START WITH id =2
- CONNECT BY PARENT_ID = PRIOR ID;
STR_LEVEL | ID | PARENT_ID | LVL |
+..B | 2 | 1 | 1 |
+....C | 3 | 2 | 2 |
+......D | 4 | 3 | 3 |
+........H | 8 | 4 | 4 |
+........I | 9 | 4 | 4 |
+......G | 7 | 3 | 3 |
+....E | 5 | 2 | 2 |
+....F | 6 | 2 | 2 |
根节点ID应该为3、5、6,即lvl为1即可
查询根节点,只显示根节点SQL
- SELECT RPAD('+', LEVEL * 2 + 1, '.') || STR STR_LEVEL,
- ID,
- PARENT_ID,
- LEVEL LVL,
- (select b.str
- from idb_hierarchical b
- where level = 1
- start with b.id = 2
- connect by prior b.id = b.parent_id
- ) root_str
- FROM idb_hierarchical I
- where level = 1
- START WITH id = 2
- CONNECT BY PARENT_ID = PRIOR ID;
- SELECT RPAD('+', LEVEL * 2 + 1, '.') || STR STR_LEVEL,
- ID,
- PARENT_ID,
- LEVEL LVL,
- (select b.str
- from idb_hierarchical b
- where level = 1
- start with b.id = 2
- connect by prior b.id = b.parent_id
- ) root_str
- FROM idb_hierarchical I
- where level = 1
- START WITH id = 2
- CONNECT BY PARENT_ID = PRIOR ID;
STR_LEVEL | ID | PARENT_ID | LVL | ROOT_STR |
+..B | 2 | 1 | 1 | B |
标明根节点SQL
- SELECT RPAD('+', LEVEL * 2 + 1, '.') || STR STR_LEVEL,
- ID,
- PARENT_ID,
- DECODE(LEVEL, 1, 'Y', 'N') is_root,
- LEVEL LVL,
- (select b.str
- from idb_hierarchical b
- where level = 1
- start with b.id = 2
- connect by prior b.id = b.parent_id) root_str
- FROM idb_hierarchical I
- START WITH id = 2
- CONNECT BY PARENT_ID = PRIOR ID;
- SELECT RPAD('+', LEVEL * 2 + 1, '.') || STR STR_LEVEL,
- ID,
- PARENT_ID,
- DECODE(LEVEL, 1, 'Y', 'N') is_root,
- LEVEL LVL,
- (select b.str
- from idb_hierarchical b
- where level = 1
- start with b.id = 2
- connect by prior b.id = b.parent_id) root_str
- FROM idb_hierarchical I
- START WITH id = 2
- CONNECT BY PARENT_ID = PRIOR ID;
STR_LEVEL | ID | PARENT_ID | IS_ROOT | LVL | ROOT_STR |
+..B | 2 | 1 | Y | 1 | B |
+....C | 3 | 2 | N | 2 | B |
+......D | 4 | 3 | N | 3 | B |
+........H | 8 | 4 | N | 4 | B |
+........I | 9 | 4 | N | 4 | B |
+......G | 7 | 3 | N | 3 | B |
+....E | 5 | 2 | N | 2 | B |
+....F | 6 | 2 | N | 2 | B |
在oracle 10g提供了connect_by_isleaf和connect_by_root
oracle 10g用connect_by_isleaf判断叶节点
- SELECT RPAD('+',LEVEL*2+1,'.')||STR STR_LEVEL,ID,PARENT_ID,LEVEL LVL
- FROM idb_hierarchical I
- where connect_by_isleaf=1
- START WITH PARENT_ID IS NULL
- CONNECT BY PARENT_ID = PRIOR ID;
- SELECT RPAD('+',LEVEL*2+1,'.')||STR STR_LEVEL,ID,PARENT_ID,LEVEL LVL
- FROM idb_hierarchical I
- where connect_by_isleaf=1
- START WITH PARENT_ID IS NULL
- CONNECT BY PARENT_ID = PRIOR ID;
STR_LEVEL | ID | PARENT_ID | LVL |
+..........H | 8 | 4 | 5 |
+..........I | 9 | 4 | 5 |
+........G | 7 | 3 | 4 |
+......E | 5 | 2 | 3 |
+......F | 6 | 2 | 3 |
+......L | 12 | 11 | 3 |
+....M | 13 | 10 | 2 |
- SELECT RPAD('+',LEVEL*2+1,'.')||STR STR_LEVEL,ID,PARENT_ID,LEVEL LVL,
- decode(connect_by_isleaf,1,'Y','N') IS_LEAF
- FROM idb_hierarchical I
- START WITH PARENT_ID IS NULL
- CONNECT BY PARENT_ID = PRIOR ID;
- SELECT RPAD('+',LEVEL*2+1,'.')||STR STR_LEVEL,ID,PARENT_ID,LEVEL LVL,
- decode(connect_by_isleaf,1,'Y','N') IS_LEAF
- FROM idb_hierarchical I
- START WITH PARENT_ID IS NULL
- CONNECT BY PARENT_ID = PRIOR ID;
STR_LEVEL | ID | PARENT_ID | LVL | IS_LEAF |
+..A | 1 | 1 | N | |
+....B | 2 | 1 | 2 | N |
+......C | 3 | 2 | 3 | N |
+........D | 4 | 3 | 4 | N |
+..........H | 8 | 4 | 5 | Y |
+..........I | 9 | 4 | 5 | Y |
+........G | 7 | 3 | 4 | Y |
+......E | 5 | 2 | 3 | Y |
+......F | 6 | 2 | 3 | Y |
+..J | 10 | 1 | N | |
+....K | 11 | 10 | 2 | N |
+......L | 12 | 11 | 3 | Y |
+....M | 13 | 10 | 2 | Y |
oracle 10g用connect_by_root判断根节点
- SELECT RPAD('+', LEVEL * 2 + 1, '.') || STR STR_LEVEL,
- ID,
- PARENT_ID,
- LEVEL LVL,
- connect_by_root STR ROOT_STR
- FROM idb_hierarchical I
- START WITH id = 2
- CONNECT BY PARENT_ID = PRIOR ID;
- SELECT RPAD('+', LEVEL * 2 + 1, '.') || STR STR_LEVEL,
- ID,
- PARENT_ID,
- LEVEL LVL,
- connect_by_root STR ROOT_STR
- FROM idb_hierarchical I
- START WITH id = 2
- CONNECT BY PARENT_ID = PRIOR ID;
STR_LEVEL | ID | PARENT_ID | LVL | ROOT_STR |
---|---|---|---|---|
+..B | 2 | 1 | 1 | B |
+....C | 3 | 2 | 2 | B |
+......D | 4 | 3 | 3 | B |
+........H | 8 | 4 | 4 | B |
+........I | 9 | 4 | 4 | B |
+......G | 7 | 3 | 3 | B |
+....E | 5 | 2 | 2 | B |
+....F | 6 | 2 | 2 | B |
- SELECT RPAD('+', LEVEL * 2 + 1, '.') || STR STR_LEVEL,
- ID,
- PARENT_ID,
- DECODE(LEVEL, 1, 'Y', 'N') is_root,
- LEVEL LVL,
- connect_by_root STR ROOT_STR
- FROM idb_hierarchical I
- START WITH id = 3
- CONNECT BY PARENT_ID = PRIOR ID;
- SELECT RPAD('+', LEVEL * 2 + 1, '.') || STR STR_LEVEL,
- ID,
- PARENT_ID,
- DECODE(LEVEL, 1, 'Y', 'N') is_root,
- LEVEL LVL,
- connect_by_root STR ROOT_STR
- FROM idb_hierarchical I
- START WITH id = 3
- CONNECT BY PARENT_ID = PRIOR ID;
STR_LEVEL | ID | PARENT_ID | IS_ROOT | LVL | ROOT_STR |
---|---|---|---|---|---|
+..C | 3 | 2 | Y | 1 | C |
+....D | 4 | 3 | N | 2 | C |
+......H | 8 | 4 | N | 3 | C |
+......I | 9 | 4 | N | 3 | C |
+....G | 7 | 3 | N | 2 | C |
- SELECT RPAD('+', LEVEL * 2 + 1, '.') || STR STR_LEVEL,
- ID,
- PARENT_ID,
- DECODE(LEVEL, 1, 'Y', 'N') is_root,
- LEVEL LVL,
- connect_by_root STR ROOT_STR
- FROM idb_hierarchical I
- START WITH PARENT_ID = 2
- CONNECT BY PARENT_ID = PRIOR ID;
- SELECT RPAD('+', LEVEL * 2 + 1, '.') || STR STR_LEVEL,
- ID,
- PARENT_ID,
- DECODE(LEVEL, 1, 'Y', 'N') is_root,
- LEVEL LVL,
- connect_by_root STR ROOT_STR
- FROM idb_hierarchical I
- START WITH PARENT_ID = 2
- CONNECT BY PARENT_ID = PRIOR ID;
STR_LEVEL | ID | PARENT_ID | IS_ROOT | LVL | ROOT_STR |
---|---|---|---|---|---|
+..C | 3 | 2 | Y | 1 | C |
+....D | 4 | 3 | N | 2 | C |
+......H | 8 | 4 | N | 3 | C |
+......I | 9 | 4 | N | 3 | C |
+....G | 7 | 3 | N | 2 | C |
+..E | 5 | 2 | Y | 1 | E |
+..F | 6 | 2 | Y | 1 | F |
【转】 oracle 层次查询判断叶子和根节点的更多相关文章
- Oracle 层次查询 connect by
oracle 层次查询 语法: SELECT ... FROM [WHERE condition] --过 ...
- 带您了解Oracle层次查询
http://database.51cto.com/art/201010/231539.htm Oracle层次查询(connect by )是结构化查询中用到的,下面就为您介绍Oracle层次查询的 ...
- Oracle层次查询
Oracle层次查询的语法如下: 下面根据两道“烧脑”的题具体来体现: 1. 根据时间先后顺序,十二星座的英文名称用逗号串起来为'Aries,Taurus,Gemini,Cancer,Leo,Virg ...
- Oracle - 层次查询
如果表中含有层次数据,可以通过使用层次查询有序地查看层次数据. 语法: condition:指一个或多个表达式和逻辑(布尔)运算符的组合,并返回TRUE.FALSE或UNKNOWNstart with ...
- Oracle层次查询start with connect by
博客参考:https://www.cnblogs.com/jerryxing/articles/2339352.html start with connect by 层次查询(Hierarchical ...
- 【转载】Oracle层次查询和分析函数
摘要 一组连续的数,去掉中间一些数,如何求出剩下的数的区间(即号段)?知道号段的起止,如何求出该号段内所有的数?知道一个大的号段范围和已经取过的号段,如何求出可用的号段?利用Oracle提供的强大的查 ...
- Oracle层次查询和分析函数在号段选取中的应用
转自:http://www.itpub.net/thread-719692-1-1.html 摘要一组连续的数,去掉中间一些数,如何求出剩下的数的区间(即号段)?知道号段的起止,如何求出该号段内所有的 ...
- oracle层次查询的陷阱
今天开发组同事找到我,说一个简单的层次查询非常慢,业务就是有一个存设备表连接关系的表,从node1连入,从node2连出,现在要找出node2的连出顺序,sql类似于: SELECT LEVEL ID ...
- Oracle层次查询和with函数的使用
开发中大家应该都做过什么类似部门管理这样的功能,一般情况下一个部门下面还有下一级部门(子部门),这个层级就类似一棵树.这种情况下一般会把父级部门和子级部门分成2个或者多个表,这种算是比较常规的做法:有 ...
随机推荐
- C#程序证书创建工具 (Makecert.exe)
原文地址:https://msdn.microsoft.com/zh-cn/library/bfsktky3(VS.80).aspx 证书创建工具生成仅用于测试目的的 X.509 证书.它创建用于数字 ...
- EF GroupBy 根据key 分组 再把key求和(取决于每条数据中 arr的条数) arr 中有多少条数据 就把多少个key 加起来
List<A> alist = new List<A>{ ,b=,c=,d=,e=}, ,b=,c=,d=,e=}, ,b=,c=,d=,e=}, ,b=,c=,d=,e=}, ...
- Android Developers:从一个Activity获取结果
启动其它Activity不是单向的.你也能启动其它Activity并获取一个返回结果.为了获取一个结果,调用startActivityForResult()方法(替代startActivity()方法 ...
- python标准库介绍——32 Queue 模块详解
Queue 模块 ``Queue`` 模块提供了一个线程安全的队列 (queue) 实现, 如 [Example 3-2 #eg-3-2] 所示. 你可以通过它在多个线程里安全访问同个对象. ==== ...
- 基础001_Xilinx V7资源
作者:桂. 时间:2018-02-08 09:37:35 链接:http://www.cnblogs.com/xingshansi/p/8430247.html 前言 本文主要是Xilinx V7系 ...
- Centos7源代码安装freeswitch和启动freeswitch
centos7下安装freeswitch版本:1.6. 1.下载源代码: 进入目录 cd /usr/local/src 克隆原代码 git clone -b v1.6 https://freeswit ...
- 使用Talend Open Studio将数据分步从oracle导入到hive中
先使用Tos建立模型,将Oracle中的数据导入到本地: build job后,形成独立可以运行的程序: 将生成的zip文件,上传到hadoop集群上,有hive环境的机器上: [hive@h1 wo ...
- msf web_delivery模块攻击
目标机:win7 ip:192.168.31.136 攻击机:kai liunx ip:192.168.31.54 一. ...
- Tcp超时修改
Linux 建立 TCP 连接的超时时间分析 tags: linux | network Linux 系统默认的建立 TCP 连接的超时时间为 127 秒,对于许多客户端来说,这个时间都太长了, 特别 ...
- centos7 安装后需要做的事情
安装centos 7 系统之后要做的几件事 #修改主机名 hostnamectl --static set-hostname xd-1 vim /etc/hosts127.0.0.1 xd-1x.x. ...