oracle 层次查询判断叶子和根节点

oracle 9i判断是叶子或根节点,是比较麻烦的一件事情,SQL演示脚本如下:

  1. DROPTABLEidb_hierarchical;
  2. createTABLEidb_hierarchical
  3. (
  4. idnumber,
  5. parent_idnumber,
  6. strvarchar2(10)
  7. );
  8. insertintoidb_hierarchicalvalues(1,null,'A');
  9. insertintoidb_hierarchicalvalues(2,1,'B');
  10. insertintoidb_hierarchicalvalues(3,2,'C');
  11. insertintoidb_hierarchicalvalues(4,3,'D');
  12. insertintoidb_hierarchicalvalues(5,2,'E');
  13. insertintoidb_hierarchicalvalues(6,2,'F');
  14. insertintoidb_hierarchicalvalues(7,3,'G');
  15. insertintoidb_hierarchicalvalues(8,4,'H');
  16. insertintoidb_hierarchicalvalues(9,4,'I');
  17. insertintoidb_hierarchicalvalues(10,null,'J');
  18. insertintoidb_hierarchicalvalues(11,10,'K');
  19. insertintoidb_hierarchicalvalues(12,11,'L');
  20. insertintoidb_hierarchicalvalues(13,10,'M');

示例数据清单如下:

  1. SELECTRPAD('+',LEVEL*2+1,'.')||STRSTR_LEVEL,ID,PARENT_ID,LEVELLVL
  2. FROMidb_hierarchical
  3. STARTWITHPARENT_IDISNULL
  4. CONNECTBYPARENT_ID=PRIORID;
表1:数据清单 STR_LEVEL ID PARENT_ID LVL
+..A 1 1
+….B 2 1 2
+……C 3 2 3
+……..D 4 3 4
+……….H 8 4 5
+……….I 9 4 5
+……..G 7 3 4
+……E 5 2 3
+……F 6 2 3
+..J 10 1
+….K 11 10 2
+……L 12 11 3
+….M 13 10 2

在表1中,ID为8、9、 7、5、6、12、13都没有子节点,因此称为叶节点。

1.oracle9i 查询叶节点

只显示叶子节点SQL

  1. SELECTRPAD('+',LEVEL*2+1,'.')||STRSTR_LEVEL,ID,PARENT_ID,LEVELLVL
  2. FROMidb_hierarchicalI
  3. --在oracle9i中显示叶节点,需要判断是否有子节点即可
  4. WHERENOTEXISTS(SELECT1
  5. FROMidb_hierarchicalB
  6. WHEREI.ID=B.PARENT_ID)
  7. STARTWITHPARENT_IDISNULL
  8. CONNECTBYPARENT_ID=PRIORID;
表2
STR_LEVEL ID PARENT_ID LVL
+……….H 8 4 5
+……….I 9 4 5
+……..G 7 3 4
+……E 5 2 3
+……F 6 2 3
+……L 12 11 3
+….M 13 10 2

显示所有节点,标明该行是否为叶节点SQL

  1. SELECTRPAD('+',LEVEL*2+1,'.')||STRSTR_LEVEL,ID,PARENT_ID,LEVELLVL,
  2. NVL((SELECT'N'
  3. FROMidb_hierarchicalB
  4. WHEREI.ID=B.PARENT_ID
  5. ANDROWNUM<2),'Y')IS_LEAF
  6. FROMidb_hierarchicalI
  7. STARTWITHPARENT_IDISNULL
  8. CONNECTBYPARENT_ID=PRIORID;
表3
STR_LEVEL ID PARENT_ID LVL IS_LEAF
+..A 1 1 N
+....B 2 1 2 N
+......C 3 2 3 N
+........D 4 3 4 N
+..........H 8 4 5 Y
+..........I 9 4 5 Y
+........G 7 3 4 Y
+......E 5 2 3 Y
+......F 6 2 3 Y
+..J 10 1 N
+....K 11 10 2 N
+......L 12 11 3 Y
+....M 13 10 2 Y

oracle 9i 查询根节点

  1. SELECTRPAD('+',LEVEL*2+1,'.')||STRSTR_LEVEL,ID,PARENT_ID,LEVELLVL
  2. FROMidb_hierarchicalI
  3. STARTWITHid=2
  4. CONNECTBYPARENT_ID=PRIORID;
表4
STR_LEVEL ID PARENT_ID LVL
+..B 2 1 1
+....C 3 2 2
+......D 4 3 3
+........H 8 4 4
+........I 9 4 4
+......G 7 3 3
+....E 5 2 2
+....F 6 2 2

根节点ID应该为3、5、6,即lvl为1即可

查询根节点,只显示根节点SQL

  1. SELECTRPAD('+',LEVEL*2+1,'.')||STRSTR_LEVEL,
  2. ID,
  3. PARENT_ID,
  4. LEVELLVL,
  5. (selectb.str
  6. fromidb_hierarchicalb
  7. wherelevel=1
  8. startwithb.id=2
  9. connectbypriorb.id=b.parent_id
  10. )root_str
  11. FROMidb_hierarchicalI
  12. wherelevel=1
  13. STARTWITHid=2
  14. CONNECTBYPARENT_ID=PRIORID;
表5
STR_LEVEL ID PARENT_ID LVL ROOT_STR
+..B 2 1 1 B

标明根节点SQL

  1. SELECTRPAD('+',LEVEL*2+1,'.')||STRSTR_LEVEL,
  2. ID,
  3. PARENT_ID,
  4. DECODE(LEVEL,1,'Y','N')is_root,
  5. LEVELLVL,
  6. (selectb.str
  7. fromidb_hierarchicalb
  8. wherelevel=1
  9. startwithb.id=2
  10. connectbypriorb.id=b.parent_id)root_str
  11. FROMidb_hierarchicalI
  12. STARTWITHid=2
  13. CONNECTBYPARENT_ID=PRIORID;
表6
STR_LEVEL ID PARENT_ID IS_ROOT LVL ROOT_STR
+..B 2 1 Y 1 B
+....C 3 2 N 2 B
+......D 4 3 N 3 B
+........H 8 4 N 4 B
+........I 9 4 N 4 B
+......G 7 3 N 3 B
+....E 5 2 N 2 B
+....F 6 2 N 2 B

在oracle 10g提供了connect_by_isleaf和connect_by_root

oracle 10g用connect_by_isleaf判断叶节点

  1. SELECTRPAD('+',LEVEL*2+1,'.')||STRSTR_LEVEL,ID,PARENT_ID,LEVELLVL
  2. FROMidb_hierarchicalI
  3. whereconnect_by_isleaf=1
  4. STARTWITHPARENT_IDISNULL
  5. CONNECTBYPARENT_ID=PRIORID;
表7
STR_LEVEL ID PARENT_ID LVL
+..........H 8 4 5
+..........I 9 4 5
+........G 7 3 4
+......E 5 2 3
+......F 6 2 3
+......L 12 11 3
+....M 13 10 2
  1. SELECTRPAD('+',LEVEL*2+1,'.')||STRSTR_LEVEL,ID,PARENT_ID,LEVELLVL,
  2. decode(connect_by_isleaf,1,'Y','N')IS_LEAF
  3. FROMidb_hierarchicalI
  4. STARTWITHPARENT_IDISNULL
  5. CONNECTBYPARENT_ID=PRIORID;
表8
STR_LEVEL ID PARENT_ID LVL IS_LEAF
+..A 1 1 N
+....B 2 1 2 N
+......C 3 2 3 N
+........D 4 3 4 N
+..........H 8 4 5 Y
+..........I 9 4 5 Y
+........G 7 3 4 Y
+......E 5 2 3 Y
+......F 6 2 3 Y
+..J 10 1 N
+....K 11 10 2 N
+......L 12 11 3 Y
+....M 13 10 2 Y

oracle 10g用connect_by_root判断根节点

  1. SELECTRPAD('+',LEVEL*2+1,'.')||STRSTR_LEVEL,
  2. ID,
  3. PARENT_ID,
  4. LEVELLVL,
  5. connect_by_rootSTRROOT_STR
  6. FROMidb_hierarchicalI
  7. STARTWITHid=2
  8. CONNECTBYPARENT_ID=PRIORID;
表9 STR_LEVEL ID PARENT_ID LVL ROOT_STR
+..B 2 1 1 B
+....C 3 2 2 B
+......D 4 3 3 B
+........H 8 4 4 B
+........I 9 4 4 B
+......G 7 3 3 B
+....E 5 2 2 B
+....F 6 2 2 B
  1. SELECTRPAD('+',LEVEL*2+1,'.')||STRSTR_LEVEL,
  2. ID,
  3. PARENT_ID,
  4. DECODE(LEVEL,1,'Y','N')is_root,
  5. LEVELLVL,
  6. connect_by_rootSTRROOT_STR
  7. FROMidb_hierarchicalI
  8. STARTWITHid=3
  9. CONNECTBYPARENT_ID=PRIORID;
表10 STR_LEVEL ID PARENT_ID IS_ROOT LVL ROOT_STR
+..C 3 2 Y 1 C
+....D 4 3 N 2 C
+......H 8 4 N 3 C
+......I 9 4 N 3 C
+....G 7 3 N 2 C
  1. SELECTRPAD('+',LEVEL*2+1,'.')||STRSTR_LEVEL,
  2. ID,
  3. PARENT_ID,
  4. DECODE(LEVEL,1,'Y','N')is_root,
  5. LEVELLVL,
  6. connect_by_rootSTRROOT_STR
  7. FROMidb_hierarchicalI
  8. STARTWITHPARENT_ID=2
  9. CONNECTBYPARENT_ID=PRIORID;
表11 STR_LEVEL ID PARENT_ID IS_ROOT LVL ROOT_STR
+..C 3 2 Y 1 C
+....D 4 3 N 2 C
+......H 8 4 N 3 C
+......I 9 4 N 3 C
+....G 7 3 N 2 C
+..E 5 2 Y 1 E
+..F 6 2 Y 1 F


你可能感兴趣的:(oracle)