Using the LEVEL Pseudo-Column:display the level in the tree : Level « Hierarchical Query « Oracle PL / SQL

Oracle PL / SQL
1. Aggregate Functions
2. Analytical Functions
3. Char Functions
4. Constraints
5. Conversion Functions
6. Cursor
7. Data Type
8. Date Timezone
9. Hierarchical Query
10. Index
11. Insert Delete Update
12. Large Objects
13. Numeric Math Functions
14. Object Oriented Database
15. PL SQL
16. Regular Expressions
17. Report Column Page
18. Result Set
19. Select Query
20. Sequence
21. SQL Plus
22. Stored Procedure Function
23. Subquery
24. System Packages
25. System Tables Views
26. Table
27. Table Joins
28. Trigger
29. User Previliege
30. View
31. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Oracle PL / SQL » Hierarchical Query » Level 
Using the LEVEL Pseudo-Column:display the level in the tree
 

SQL>
SQL> CREATE TABLE employee (
  2    employee_id INTEGER,
  3    manager_id INTEGER,
  4    first_name VARCHAR2(10NOT NULL,
  5    last_name VARCHAR2(10NOT NULL,
  6    title VARCHAR2(20),
  7    salary NUMBER(60)
  8  );

Table created.

SQL>
SQL>
SQL> insert into employee (EMPLOYEE_ID, MANAGER_ID,FIRST_NAME,LAST_NAME,TITLE,SALARY)
  2                 values1         ,0            'James'  ,'Smith'  ,'CEO',800000);

row created.

SQL>
SQL> insert into employee (EMPLOYEE_ID, MANAGER_ID,FIRST_NAME,LAST_NAME,TITLE,SALARY)
  2                 values2         1         ,'Ron'     ,'Johnson','Sales Manager',600000);

row created.

SQL>
SQL> insert into employee (EMPLOYEE_ID, MANAGER_ID,FIRST_NAME,LAST_NAME,TITLE,SALARY)
  2                 values3         2         ,'Fred'    ,'Hobbs'  ,'Sales Person',200000);

row created.

SQL>
SQL> insert into employee (EMPLOYEE_ID, MANAGER_ID,FIRST_NAME,LAST_NAME,TITLE,SALARY)
  2                 values4         1         ,'Susan'   ,'Jones'  ,'Support Manager',500000);

row created.

SQL>
SQL> insert into employee (EMPLOYEE_ID, MANAGER_ID,FIRST_NAME,LAST_NAME,TITLE,SALARY)
  2                 values5         2         ,'Rob'     ,'Green'  ,'Sales Person', 40000);

row created.

SQL>
SQL> insert into employee (EMPLOYEE_ID, MANAGER_ID,FIRST_NAME,LAST_NAME,TITLE,SALARY)
  2                 values6         4         ,'Jane'    ,'Brown'  ,'Support Person',45000);

row created.

SQL>
SQL> insert into employee (EMPLOYEE_ID, MANAGER_ID,FIRST_NAME,LAST_NAME,TITLE,SALARY)
  2                 values7         4         ,'John'    ,'Grey'   ,'Support Manager',30000);

row created.

SQL>
SQL> insert into employee (EMPLOYEE_ID, MANAGER_ID,FIRST_NAME,LAST_NAME,TITLE,SALARY)
  2                 values8         7         ,'Jean'    ,'Blue'   ,'Support Person',29000);

row created.

SQL>
SQL> insert into employee (EMPLOYEE_ID, MANAGER_ID,FIRST_NAME,LAST_NAME,TITLE,SALARY)
  2                 values9         6         ,'Henry'   ,'Heyson' ,'Support Person',30000);

row created.

SQL>
SQL> insert into employee (EMPLOYEE_ID, MANAGER_ID,FIRST_NAME,LAST_NAME,TITLE,SALARY)
  2                 values10        1         ,'Kevin'   ,'Black'  ,'Ops Manager',100000);

row created.

SQL>
SQL> insert into employee (EMPLOYEE_ID, MANAGER_ID,FIRST_NAME,LAST_NAME,TITLE,SALARY)
  2                 values11        10        ,'Keith'   ,'Long'   ,'Ops Person',50000);

row created.

SQL>
SQL>
SQL> insert into employee (EMPLOYEE_ID, MANAGER_ID,FIRST_NAME,LAST_NAME,TITLE,SALARY)
  2                 values12        10        ,'Frank'   ,'Howard' ,'Ops Person',45000);

row created.

SQL>
SQL> insert into employee (EMPLOYEE_ID, MANAGER_ID,FIRST_NAME,LAST_NAME,TITLE,SALARY)
  2                 values13        10        ,'Doreen'  ,'Penn'   ,'Ops Person',47000);

row created.

SQL>
SQL>
SQL>
SQL>
SQL> select from employee;

EMPLOYEE_ID MANAGER_ID FIRST_NAME LAST_NAME  TITLE                    SALARY
----------- ---------- ---------- ---------- -------------------- ----------
          1          0 James      Smith      CEO                      800000
          2          1 Ron        Johnson    Sales Manager            600000
          3          2 Fred       Hobbs      Sales Person             200000
          4          1 Susan      Jones      Support Manager          500000
          5          2 Rob        Green      Sales Person              40000
          6          4 Jane       Brown      Support Person            45000
          7          4 John       Grey       Support Manager           30000
          8          7 Jean       Blue       Support Person            29000
          9          6 Henry      Heyson     Support Person            30000
         10          1 Kevin      Black      Ops Manager              100000
         11         10 Keith      Long       Ops Person                50000
         12         10 Frank      Howard     Ops Person                45000
         13         10 Doreen     Penn       Ops Person                47000

13 rows selected.

SQL>
SQL>
SQL> --Using the LEVEL Pseudo-Column:display the level in the tree
SQL>
SQL> SELECT LEVEL, employee_id, manager_id, first_name, last_name
  2  FROM employee
  3  START WITH employee_id = 1
  4  CONNECT BY PRIOR employee_id = manager_id
  5  ORDER BY LEVEL;

     LEVEL EMPLOYEE_ID MANAGER_ID FIRST_NAME LAST_NAME
---------- ----------- ---------- ---------- ----------
         1           1          0 James      Smith
         2          10          1 Kevin      Black
         2           2          1 Ron        Johnson
         2           4          1 Susan      Jones
         3          13         10 Doreen     Penn
         3           7          4 John       Grey
         3          11         10 Keith      Long
         3           5          2 Rob        Green
         3           3          2 Fred       Hobbs
         3          12         10 Frank      Howard
         3           6          4 Jane       Brown
         4           8          7 Jean       Blue
         4           9          6 Henry      Heyson

13 rows selected.

SQL>
SQL>
SQL>
SQL> drop table employee;

Table dropped.

SQL>
SQL>

           
         
  
Related examples in the same category
1. Use the COUNT() function and LEVEL to get the number of levels in the tree
2. Traversing Upward Through the Tree
3. sort by LEVEL
4. pseudocolumn LEVEL and an example of using the levels with an update.
5. pseudocolumn LEVEL and an example of using the levels.
6. Browse Products with three level nested queries
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.