如何遍历Oracle中的分层数据?
问题陈述:您需要从上到下遍历层次结构数据,以标记层次结构中每一行的级别。
解:
Oracle提供CONNECTBY子句以指定分层查询,即如何连接父节点和子节点,以及PRIOR运算符以定义父节点之间的连接条件,以及LEVEL伪列以指示距根节点多远。当前行是父行。
此外,我们可以使用STARTWITH子句来指示从何处开始树导航。我们必须使用PRIOR运算符在父行中指定与子行中的链接列具有相同值的列。
为了遍历整个层次结构,请删除STARTWITH子句。这将使所有学生及其教授和教授的教授(即院长),等等,直到研究所的最高经理/主任。
示例1:确定Professor_id=100SQL的层次结构/结构:
/* Function - Identify the hierarchical/structure for the professor_id = 102 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 = 102 CONNECT BY professor_id = PRIOR student_id;
输出结果
103 1 BROWN MICHAEL 104 2 -JONES WILLIAM 105 2 -MILLER DAVID 106 2 -DAVIS RICHARD 107 2 -GARCIA CHARLES
示例2:标识Professor/id为NULL的层次结构/结构SQL:
/* Function - Identify the hierarchical/structure professor_id is null 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;
输出:SQL上面的行很少
100 1 SMITH JAMES 101 2 -JOHNSON JOHN 108 3 -RODRIGUEZ JOSEPH 109 4 -WILSON THOMAS 110 4 -MARTINEZ CHRISTOPHER 111 4 -ANDERSON DANIEL 112 4 -TAYLOR PAUL 113 4 -THOMAS MARK 102 2 -WILLIAMS ROBERT 103 3 -BROWN MICHAEL 104 4 -JONES WILLIAM 105 4 -MILLER DAVID 106 4 -DAVIS RICHARD 107 4 -GARCIA CHARLES
示例3:确定研究所的整个层次结构/结构SQL:
/* Function - Identify the Institute entire hierarchical/structure Tables Used - students Data - documented below */ SELECT student_id, level, lpad('-',(level-1)*3) || first_name || ' ' || last_name AS full_name FROM students CONNECT BY student_id = PRIOR professor_id;
输出:SQL上面的行很少
100 1 SMITH JAMES 101 1 JOHNSON JOHN 100 2 -SMITH JAMES 102 1 WILLIAMS ROBERT 100 2 -SMITH JAMES 103 1 BROWN MICHAEL 102 2 -WILLIAMS ROBERT 100 3 -SMITH JAMES 104 1 JONES WILLIAM 103 2 -BROWN MICHAEL 102 3 -WILLIAMS ROBERT 100 4 -SMITH JAMES 105 1 MILLER DAVID 103 2 -BROWN MICHAEL 102 3 -WILLIAMS ROBERT 100 4 -SMITH JAMES 106 1 DAVIS RICHARD 103 2 -BROWN MICHAEL 102 3 -WILLIAMS ROBERT 100 4 -SMITH JAMES 107 1 GARCIA CHARLES 103 2 -BROWN MICHAEL 102 3 -WILLIAMS ROBERT 100 4 -SMITH JAMES 108 1 RODRIGUEZ JOSEPH 101 2 -JOHNSON JOHN 100 3 -SMITH JAMES 109 1 WILSON THOMAS 108 2 -RODRIGUEZ JOSEPH 101 3 -JOHNSON JOHN 100 4 -SMITH JAMES 110 1 MARTINEZ CHRISTOPHER 108 2 -RODRIGUEZ JOSEPH 101 3 -JOHNSON JOHN 100 4 -SMITH JAMES 111 1 ANDERSON DANIEL 108 2 -RODRIGUEZ JOSEPH 101 3 -JOHNSON JOHN 100 4 -SMITH JAMES 112 1 TAYLOR PAUL 108 2 -RODRIGUEZ JOSEPH 101 3 -JOHNSON JOHN 100 4 -SMITH JAMES 113 1 THOMAS MARK 108 2 -RODRIGUEZ JOSEPH 101 3 -JOHNSON JOHN 100 4 -SMITH JAMES
数据准备:用于该问题的数据如下所示。数据完全用于演示目的。
示例
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','SMITH.JAMES@notreal.com','111.111.1245',TO_DATE('17-06-2003','DD-MM-YYYY'),'INS_CHAIRMAN',24000,NULL,NULL); INSERT INTO students VALUES (101,'JOHNSON','JOHN','JOHNSON.JOHN@notreal.com','111.111.1246',TO_DATE('21-09-2005','DD-MM-YYYY'),'INS_VP',17000,100,90); INSERT INTO students VALUES (102,'WILLIAMS','ROBERT','WILLIAMS.ROBERT@notreal.com','111.111.1247',TO_DATE('13-01-2001','DD-MM-YYYY'),'INS_VP',17000,100,90); INSERT INTO students VALUES (103,'BROWN','MICHAEL','BROWN.MICHAEL@notreal.com','111.111.1248',TO_DATE('03-01-2006','DD-MM-YYYY'),'INS_STAFF',9000,102,60); INSERT INTO students VALUES (104,'JONES','WILLIAM','JONES.WILLIAM@notreal.com','111.111.1249',TO_DATE('21-05-2007','DD-MM-YYYY'),'INS_STAFF',6000,103,60); INSERT INTO students VALUES (105,'MILLER','DAVID','MILLER.DAVID@notreal.com','111.111.1250',TO_DATE('25-06-2005','DD-MM-YYYY'),'INS_STAFF',4800,103,60); INSERT INTO students VALUES (106,'DAVIS','RICHARD','DAVIS.RICHARD@notreal.com','111.111.1251',TO_DATE('05-02-2006','DD-MM-YYYY'),'INS_STAFF',4800,103,60); INSERT INTO students VALUES (107,'GARCIA','CHARLES','GARCIA.CHARLES@notreal.com','111.111.1252',TO_DATE('07-02-2007','DD-MM-YYYY'),'INS_STAFF',4200,103,60); INSERT INTO students VALUES (108,'RODRIGUEZ','JOSEPH','RODRIGUEZ.JOSEPH@notreal.com','111.111.1253',TO_DATE('17-08-2002','DD-MM-YYYY'),'CL_PHY',12008,101,100); INSERT INTO students VALUES (109,'WILSON','THOMAS','WILSON.THOMAS@notreal.com','111.111.1254',TO_DATE('16-08-2002','DD-MM-YYYY'),'CL_MATH',9000,108,100); INSERT INTO students VALUES (110,'MARTINEZ','CHRISTOPHER','MARTINEZ.CHRISTOPHER@notreal.com','111.111.1255',TO_DATE('28-09-2005','DD-MM-YYYY'),'CL_MATH',8200,108,100); INSERT INTO students VALUES (111,'ANDERSON','DANIEL','ANDERSON.DANIEL@notreal.com','111.111.1256',TO_DATE('30-09-2005','DD-MM-YYYY'),'CL_MATH',7700,108,100); INSERT INTO students VALUES (112,'TAYLOR','PAUL','TAYLOR.PAUL@notreal.com','111.111.1257',TO_DATE('07-03-2006','DD-MM-YYYY'),'CL_MATH',7800,108,100); INSERT INTO students VALUES (113,'THOMAS','MARK','THOMAS.MARK@notreal.com','111.111.1258',TO_DATE('07-12-2007','DD-MM-YYYY'),'CL_MATH',6900,108,100); COMMIT;