如何在 Oracle 数据库的层次数据中删除一个分支?


问题陈述:你需要自顶向下遍历层次数据,但不想在输出中包含特定分支。

解决方案:我们将研究此问题陈述的几个示例。

Oracle 提供 CONNECT BY 子句来指定层次查询,即如何连接父节点和子节点,以及 PRIOR 运算符来定义父节点之间的连接条件,以及 LEVEL 伪列来指示当前行距离根/父行的距离。

此外,我们可以使用 START WITH 子句来指示树导航的起始位置。我们必须使用 PRIOR 运算符来指定父行中与子行中的链接列具有相同值的列。

为了遍历整个层次结构,请移除 START WITH 子句。这将获取所有学生及其经理、经理的经理等等,直到顶级经理/主管。

在这个例子中,我将使用学生数据来演示问题的解决方案。

示例

SELECT student_id, first_name, last_name FROM students ; 

输出

100 SMITH       JAMES 101 JOHNSON     JOHN 102 WILLIAMS    ROBERT 103 BROWN       MICHAEL 104 JONES       WILLIAM 105 MILLER      DAVID

假设你不想在这个报告中看到“BROWN MICHAEL”及其下属,这意味着我们需要从树中删除/修剪整个分支。为此,我们必须在 CONNECT BY 子句中将要删除的分支的顶部作为附加条件过滤掉,如下所示。

SQL

/*
     Function - Remove the branch - "BROWN MICHAEL" from the output
  Tables Used - students
         Data - Documented below
*/
SELECT student_id,
       level,
       lpad('-',(level-1)*3) || first_name || ' ' || last_name AS full_name
  FROM students
START WITH professor_id IS null
CONNECT BY professor_id = PRIOR student_id
AND NOT (first_name || ' ' || last_name = 'BROWN MICHAEL' )
ORDER SIBLINGS by first_name, last_name;

输出

100 1   SMITH JAMES 101 2     -JOHNSON JOHN 108 3        -RODRIGUEZ JOSEPH 111 4           -ANDERSON DANIEL 110 4           -MARTINEZ CHRISTOPHER 112 4           -TAYLOR PAUL 113 4           -THOMAS MARK 109 4           -WILSON THOMAS 102 2     -WILLIAMS ROBERT

如果我们只想忽略父节点,但仍然包含子节点,即排除“BROWN MICHAEL”,但仍然包含其下属学生,则过滤器应该位于 WHERE 子句中,如下所示

SQL

/*
     Function - Remove only the branch head - BROWN MICHAEL from the output
  Tables Used - students
         Data - Documented below
*/
SELECT student_id,
       level,
       lpad('-',(level-1)*3) || first_name || ' ' || last_name AS full_name
  FROM students
WHERE NOT (first_name || ' ' || last_name = 'BROWN MICHAEL' )
START WITH professor_id IS null
CONNECT BY professor_id = PRIOR student_id
ORDER SIBLINGS by first_name, last_name;

输出

100 1   SMITH JAMES 101 2     -JOHNSON JOHN 108 3        -RODRIGUEZ JOSEPH 111 4           -ANDERSON DANIEL 110 4           -MARTINEZ CHRISTOPHER 112 4           -TAYLOR PAUL 113 4           -THOMAS MARK 109 4           -WILSON THOMAS 102 2     -WILLIAMS ROBERT 106 4           -DAVIS RICHARD 107 4           -GARCIA CHARLES 104 4           -JONES WILLIAM

数据准备:用于问题的示例数据如下所示。数据完全是为了演示目的而编造的。

示例

DROP TABLE students;
COMMIT;

CREATE TABLE students
    ( student_id     NUMBER(6)
    , first_name     VARCHAR2(20)
    , last_name      VARCHAR2(25)
    , email          VARCHAR2(40)
    , phone_number   VARCHAR2(20)
    , join_date      DATE
    , class_id       VARCHAR2(20)  
    , fees           NUMBER(8,2)
    , professor_id   NUMBER(6)
    , department_id  NUMBER(4)
    ) ;

示例

CREATE UNIQUE INDEX stu_id_pk ON students (student_id) ;
INSERT INTO students VALUES (100,'SMITH','JAMES','[email protected]','111.111.1245',TO_DATE('17-06-2003','DD-MM-YYYY'),'INS_CHAIRMAN',24000,NULL,NULL);
INSERT INTO students VALUES (101,'JOHNSON','JOHN','[email protected]','111.111.1246',TO_DATE('21-09-2005','DD-MM-YYYY'),'INS_VP',17000,100,90);
INSERT INTO students VALUES (102,'WILLIAMS','ROBERT','[email protected]','111.111.1247',TO_DATE('13-01-2001','DD-MM-YYYY'),'INS_VP',17000,100,90);
INSERT INTO students VALUES (103,'BROWN','MICHAEL','[email protected]','111.111.1248',TO_DATE('03-01-2006','DD-MM-YYYY'),'INS_STAFF',9000,102,60);
INSERT INTO students VALUES (104,'JONES','WILLIAM','[email protected]','111.111.1249',TO_DATE('21-05-2007','DD-MM-YYYY'),'INS_STAFF',6000,103,60);
INSERT INTO students VALUES (105,'MILLER','DAVID','[email protected]','111.111.1250',TO_DATE('25-06-2005','DD-MM-YYYY'),'INS_STAFF',4800,103,60);
INSERT INTO students VALUES (106,'DAVIS','RICHARD','[email protected]','111.111.1251',TO_DATE('05-02-2006','DD-MM-YYYY'),'INS_STAFF',4800,103,60);
INSERT INTO students VALUES (107,'GARCIA','CHARLES','[email protected]','111.111.1252',TO_DATE('07-02-2007','DD-MM-YYYY'),'INS_STAFF',4200,103,60);
INSERT INTO students VALUES (108,'RODRIGUEZ','JOSEPH','[email protected]','111.111.1253',TO_DATE('17-08-2002','DD-MM-YYYY'),'CL_PHY',12008,101,100);
INSERT INTO students VALUES (109,'WILSON','THOMAS','[email protected]','111.111.1254',TO_DATE('16-08-2002','DD-MM-YYYY'),'CL_MATH',9000,108,100);
INSERT INTO students VALUES (110,'MARTINEZ','CHRISTOPHER','[email protected]','111.111.1255',TO_DATE('28-09-2005','DD-MM-YYYY'),'CL_MATH',8200,108,100);
INSERT INTO students VALUES (111,'ANDERSON','DANIEL','[email protected]','111.111.1256',TO_DATE('30-09-2005','DD-MM-YYYY'),'CL_MATH',7700,108,100);
INSERT INTO students VALUES (112,'TAYLOR','PAUL','[email protected]','111.111.1257',TO_DATE('07-03-2006','DD-MM-YYYY'),'CL_MATH',7800,108,100);
INSERT INTO students VALUES (113,'THOMAS','MARK','[email protected]','111.111.1258',TO_DATE('07-12-2007','DD-MM-YYYY'),'CL_MATH',6900,108,100);

COMMIT;

更新于:2020-12-04

459 次查看

启动你的职业生涯

通过完成课程获得认证

开始
广告