博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ORACLE 查询上级下级间关系
阅读量:4983 次
发布时间:2019-06-12

本文共 2413 字,大约阅读时间需要 8 分钟。

WITH TMP AS       (SELECT TMP.ORG_NUM AS INST_ID,               TMP.UP_ORG_NUM AS PARENT_INST_ID,               LTRIM(SYS_CONNECT_BY_PATH(TMP.ORG_NUM, ','), ',') AS CODE_PATH        FROM (SELECT A.ORG_NUM,                     CASE WHEN A.ORG_TYP = '0' THEN NULL ELSE A.UP_ORG_NUM END AS UP_ORG_NUM,                     A.ORG_TYP                FROM TESTNOW A               WHERE A.DATA_DATE = 20180430) TMP        START WITH ORG_TYP = '0'        CONNECT BY PRIOR TMP.ORG_NUM = TMP.UP_ORG_NUM        UNION ALL        SELECT TMP.ORG_NUM AS INST_ID,               TMP.UP_ORG_NUM AS PARENT_INST_ID,               LTRIM(SYS_CONNECT_BY_PATH(TMP.ORG_NUM, ','), ',') AS CODE_PATH        FROM (SELECT A.ORG_NUM,                     CASE WHEN A.ORG_TYP = '9' THEN NULL ELSE A.UP_ORG_NUM END AS UP_ORG_NUM,                     A.ORG_TYP                FROM TESTNOW A               WHERE A.DATA_DATE = 20180430) TMP        START WITH ORG_TYP = '9'        CONNECT BY PRIOR TMP.ORG_NUM = TMP.UP_ORG_NUM)       SELECT SUBSTR(',' || A.CODE_PATH || ',', INSTR(',' || A.CODE_PATH, ',', 1, B.RN) + 1, INSTR(A.CODE_PATH || ',', ',', 1, B.RN) - INSTR(',' || A.CODE_PATH, ',', 1, B.RN)) AS INST_ID,             A.INST_ID AS SUB_INST_ID,             CASE               WHEN B.RN = 1 THEN                'root'               ELSE                SUBSTR(',' || A.CODE_PATH || ',', INSTR(',' || A.CODE_PATH, ',', 1, B.RN - 1) + 1, INSTR(A.CODE_PATH || ',', ',', 1, B.RN - 1) - INSTR(',' || A.CODE_PATH, ',', 1, B.RN - 1))             END UP_INST_ID,             CODE_PATH      FROM TMP A,           (SELECT ROWNUM RN            FROM DUAL            CONNECT BY ROWNUM < 10) B      WHERE LENGTH(A.CODE_PATH) - LENGTH(REPLACE(A.CODE_PATH, ',')) + 1 >= B.RN;

select connect_by_root t.bank_org_code hq_bank_org_cd,

connect_by_root t.branch_cd hq_branch_cd,
t.branch_cd,
t.busi_dt,
t.org_type_cd,
t.org_type_name,
t.branch_cd_org
from TESTDWH t
where t.busi_dt = date '2018-04-30'
start with org_type_cd = '0'
connect by nocycle prior t.branch_cd_org = t.branch_cd_up
union all
select connect_by_root t2.bank_org_code hq_bank_org_cd,
connect_by_root t2.branch_cd hq_branch_cd,
t2.branch_cd,
t2.busi_dt,
t2.org_type_cd,
t2.org_type_name,
t2.branch_cd_org
from TESTDWH t2
where t2.busi_dt = date '2018-04-30'
start with org_type_cd = '9'
connect by nocycle prior t2.branch_cd_org = t2.branch_cd_up;

 

 

转载于:https://www.cnblogs.com/ayumie/p/9922853.html

你可能感兴趣的文章
Thinkphp中文水印和图片水印合体集成插件
查看>>
FLASK安装--兼收EZ_INSTALL及PIP
查看>>
C++静态成员变量和静态成员函数小结
查看>>
Python---Flask--02--模板
查看>>
PHP学习笔记---封装(面向对象三大特性之一)
查看>>
如何快速找到指定端口被哪个程序占用并释放该端口(解决bindException)
查看>>
迭代之while循环(1)
查看>>
final修饰的类有什么特点
查看>>
关于string类中find函数的讲解
查看>>
程序员的情书
查看>>
Spring Cloud Eureka 使用 IP 地址进行服务注册
查看>>
Python 包的制作(__init__.py)
查看>>
java内存模型优化建议
查看>>
三十、模块补充
查看>>
流程审批设计
查看>>
别装了,你根本就不想变成更好的人
查看>>
数据库 join
查看>>
AES加密工具类[亲测可用]
查看>>
方法区
查看>>
Django-----ORM
查看>>