Oracle 层次查询 connect by
oracle 层次查询
select *
from myemp
start with employee_id = 101
connect by prior employee_id = manager_id; -- 找下属
-- connect by employee_id = prior manager_id; --找上司
SELECT COUNT(DISTINCT LEVEL)
FROM EMPLOYEES
START WITH MANAGER_ID IS NULL
CONNECT BY PRIOR EMPLOYEE_ID = MANAGER_ID;
select count(level) --在统计树种节点的数量时, 一定不能加distinct!
from employees
start with manager_id is null
connect by prior employee_id = manager_id
group by level;
delete from myemp
where employee_id in ( select employee_id
from myemp
start with last_name = 'Kochhar'
connect by prior employee_id = manager_id);
过滤某些结果集
场景1:使用 where 过滤某些节点 , 注意不是过滤分支!
select level, employee_id, last_name, manager_id
from employees
where level = 2 --注意where子句出现的位置
start with manager_id is null
connect by prior employee_id = manager_id;
SELECT *
FROM employees
WHERE last_name = 'Mavris'
START WITH last_name = 'Kochhar' --Kochhar的所有雇员
CONNECT BY PRIOR employee_id = manager_id;
场景2: 使用 connect by ... and ... 过滤某些分支
例1 查询Raphaely及其的所有下属
select *
from employees
start with last_name = 'Raphaely'
connect by prior employee_id = manager_id;
例2 查询除了Raphaely和他下属的所有员工
select *
from employees
start with manager_id is null
connect by prior employee_id = manager_id
and last_name <> 'Raphaely';
格式化查询 lpad('-', 3 * (level - 1), '-')
select *
from employees
start with manager_id is null
connect by prior employee_id = manager_id
and last_name <> 'Raphaely';
SYS_CONNECT_BY_PATH() 函数 ☆
CONNECT_BY_ISLEAF 伪列
CONNECT_BY_ROOT 字段x -> 找到该节点最顶端节点的字段x
select last_name "Employee",
connect_by_root last_name "Manager",
sys_connect_by_path(last_name, ' -> ') "Path"
from hr.employees
where level > 1
-- start with 加不加??
connect by prior employee_id = manager_id
order by last_name, length("Path");
10g新特性 采用sibilings排序
select t.employee_id,t.manager_id,t.first_name,t.salary,sys_connect_by_path(t.first_name, '->'),level
from hr.employees t
start with manager_id is null
connect by prior employee_id = manager_id
order by salary desc;
采用sibilings排序:
select t.employee_id,
t.manager_id,
t.first_name,
t.salary,
sys_connect_by_path(t.first_name, '->'),
level
from hr.employees t
start with manager_id is null
connect by prior employee_id = manager_id
order siblings by salary desc;
Oracle 层次查询 connect by的更多相关文章
- Oracle层次查询
Oracle层次查询的语法如下: 下面根据两道“烧脑”的题具体来体现: 1. 根据时间先后顺序,十二星座的英文名称用逗号串起来为'Aries,Taurus,Gemini,Cancer,Leo,Virg ...
- 带您了解Oracle层次查询
http://database.51cto.com/art/201010/231539.htm Oracle层次查询(connect by )是结构化查询中用到的,下面就为您介绍Oracle层次查询的 ...
- .Net程序员学用Oracle系列(20):层次查询(CONNECT BY)
1.层次查询语句 1.1.CONNECT BY 语法 1.2.CONNECT BY 示例 2.层次查询函数 2.1.SYS_CONNECT_BY_PATH 2.2.WMSYS.WM_CONCAT 2. ...
- 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 - 层次查询
如果表中含有层次数据,可以通过使用层次查询有序地查看层次数据. 语法: condition:指一个或多个表达式和逻辑(布尔)运算符的组合,并返回TRUE.FALSE或UNKNOWNstart with ...
- oracle层次查询的陷阱
今天开发组同事找到我,说一个简单的层次查询非常慢,业务就是有一个存设备表连接关系的表,从node1连入,从node2连出,现在要找出node2的连出顺序,sql类似于: SELECT LEVEL ID ...
- 【转】 oracle 层次查询判断叶子和根节点
Oracle 9i判断是叶子或根节点,是比较麻烦的一件事情,SQL演示脚本如下: DROP TABLE idb_hierarchical; create TABLE idb_hierarchical ...
随机推荐
- 立即执行函数(function(){})()与闭包
立即执行函数 匿名(function(){})() 当一个匿名函数被括起来,然后再在后面加一个括号,这个匿名函数就能立即运行起来. $(function(){}) $(function(){});是$ ...
- flask中的static_path和static_path_url和static_folder
static_folder表示静态文件所在路径,默认为root_dir下的static文件夹 static_url_path的行为比较复杂 如果static_folder未被指定(也就是默认值stat ...
- cache verilog实现
cache原理: https://www.cnblogs.com/mikewolf2002/p/10984976.html cache的verilog实现 实现的cache是16k, 4way组相连c ...
- 【转】LockSupport深入浅出
原文:https://www.cnblogs.com/qingquanzi/p/8228422.html 本篇是<自己动手写把"锁">系列技术铺垫的最后一个知识点.本篇 ...
- android 实时获取网速
public class NetSpeed { private static final String TAG = NetSpeed.class.getSimpleName(); private lo ...
- elastalert docker安装
基于对elasticsearch中数据监控需要,我尝试了sentinl和elastalert两款工具.虽然elastalert是纯文本,但易配置管理.elk自带的watch需要付费才可使用. 6.2x ...
- Eric6安装问题解决
按照http://eric-ide.python-projects.org/eric-download.html中的说明,执行命令:python install.py 却遇到下面的问题: Compil ...
- Jenkins+SVN+Ant在Linux环境下自动完成版本的增量更新与编译
第一步:查看安装的jdk版本,查看是否安装ant,查看是否安装Jenkins java -version ant -version rpm -qa|grep jenkins 第二步:安装ant 官网: ...
- Spatial Temporal Graph Convolutional Networks for Skeleton-Based Action Recognition (ST-GCN)
Spatial Temporal Graph Convolutional Networks for Skeleton-Based Action Recognition 摘要 动态人体骨架模型带有进行动 ...
- 使用istioctl命令查看gateway及virtualservices
istioctl命令,比kubectl命令,在查看istio资源方面,要方便很多. 如果使用microk8s安装,则命令为microk8s.istioctl了. 查看gateway及virtualse ...