Select Tree Node

时间:2022-05-09 05:05:25

这里用到了Oracle的一个树形结构查询函数select *  from record START WITH A.TREE_NODE IN ('COST_CTR_10053')
CONNECT BY PRIOR A.TREE_NODE_NUM = A.PARENT_NODE_NUM.查出指定节点下的所以子节点然后连接leaf表,选出所有的叶子。

/* Formatted on 11/7/2015 11:05:57 PM (QP5 v5.267.14150.38573) */
SELECT B.RANGE_FROM
FROM PSTREELEAF B,
( SELECT DISTINCT A.SETID,
A.SETCNTRLVALUE,
A.TREE_NAME,
A.EFFDT,
A.TREE_NODE_NUM
FROM PSTREENODE A,
(SELECT A.SETID,
A.SETCNTRLVALUE,
A.TREE_NAME,
A.EFFDT
FROM PSTREEDEFN A
WHERE A.SETID = 'SHARE'
AND A.SETCNTRLVALUE = ' '
AND A.TREE_NAME = 'PLD_LOC_COMBO'
AND A.EFFDT =
(SELECT MAX (B.EFFDT)
FROM PSTREEDEFN B
WHERE A.SETID = B.SETID
AND A.SETCNTRLVALUE = B.SETCNTRLVALUE
AND A.TREE_NAME = B.TREE_NAME
AND B.EFF_STATUS = 'A'
AND B.EFFDT <= SYSDATE)) DEFN
WHERE A.SETID = DEFN.SETID
AND A.SETCNTRLVALUE = DEFN.SETCNTRLVALUE
AND A.TREE_NAME = DEFN.TREE_NAME
AND A.EFFDT = DEFN.EFFDT
START WITH A.TREE_NODE IN ('COST_CTR_10053')
CONNECT BY PRIOR A.TREE_NODE_NUM = A.PARENT_NODE_NUM) SA
WHERE SA.SETID = B.SETID
AND SA.SETCNTRLVALUE = B.SETCNTRLVALUE
AND SA.TREE_NAME = B.TREE_NAME
AND SA.EFFDT = B.EFFDT
AND SA.TREE_NODE_NUM = B.TREE_NODE_NUM

************************************************************************************************************************************************

SELECT P2.*
FROM
(SELECT A.SETID ,
A.SETCNTRLVALUE ,
A.TREE_NAME ,
A.EFFDT
FROM PSTREEDEFN A
WHERE A.SETID = 'SHARE'
AND A.SETCNTRLVALUE = ' '
AND A.TREE_NAME = 'PLD_LOC_COMBO'
AND A.EFFDT =
(SELECT MAX (B.EFFDT)
FROM PSTREEDEFN B
WHERE A.SETID = B.SETID
AND A.SETCNTRLVALUE = B.SETCNTRLVALUE
AND A.TREE_NAME = B.TREE_NAME
AND B.EFF_STATUS = 'A'
AND B.EFFDT <= SYSDATE)) P,
PSTREENODE P1 ,
PSTREELEAF P2
WHERE P1.SETID = P.SETID
AND P1.SETCNTRLVALUE = P.SETCNTRLVALUE
AND P1.TREE_NAME = P.TREE_NAME
AND P1.EFFDT = P.EFFDT
AND P1.TREE_NODE = 'COST_CTR_10053'
AND P2.SETID = P1.SETID
AND P2.SETCNTRLVALUE = P1.SETCNTRLVALUE
AND P2.TREE_NAME = P1.TREE_NAME
AND P2.EFFDT = P1.EFFDT
-- AND P2.TREE_NODE_NUM BETWEEN P1.TREE_NODE_NUM AND P1.TREE_NODE_NUM_END
AND P2.TREE_NODE_NUM = P1.TREE_NODE_NUM

AND (
( NVL(LENGTH(REPLACE(TRANSLATE('143','0123456789.',' '),' ','')), 0) <> 0 AND '143' BETWEEN P2.RANGE_FROM AND P2.RANGE_TO)
OR
( NVL(LENGTH(REPLACE(TRANSLATE('143','0123456789.',' '),' ','')), 0) = 0 AND '143' BETWEEN P2.RANGE_FROM AND P2.RANGE_TO AND LENGTH('143') BETWEEN LENGTH(P2.RANGE_FROM) AND LENGTH(P2.RANGE_TO))
)