Formatting the Results from a Hierarchical Query


CREATE TABLE employees
( employee_id          number(10)      not null,
  last_name            varchar2(50)      not null,
  job_id               varchar2(30),
  department_id        number(10),
  salary               number(6),
  manager_id           number(6)
);
insert into employees( employee_id, last_name, job_id, salary,department_id ,manager_id)
values(                1001,        'Lawson',  'MGR',  30000, 1,             1004);
insert into employees( employee_id, last_name, job_id, salary, department_id ,manager_id)
values(                1002,        'Wells',   'DBA',  20000, 2,             1005 );
insert into employees( employee_id, last_name, job_id, salary, department_id ,manager_id)
values(                1003,        'Bliss',   'PROG', 24000, 3,             1004);
insert into employees( employee_id, last_name, job_id, salary, department_id, manager_id)
values(                1004,        'Kyte',    'MGR',  25000 ,4,             1005);
insert into employees( employee_id, last_name, job_id, salary, department_id, manager_id)
values(                1005,        'Viper',   'PROG', 20000, 1,             1006);
insert into employees( employee_id, last_name, job_id, salary, department_id,manager_id)
values(                1006,        'Beck',    'PROG', 20000, 2,             null);
insert into employees( employee_id, last_name, job_id, salary, department_id, manager_id)
values(                1007,        'Java',    'PROG', 20000, 3,             1006);
insert into employees( employee_id, last_name, job_id, salary, department_id, manager_id)
values(                1008,        'Oracle',  'DBA',  20000, 4,             1006);




SQL> SELECT LEVEL,
  2  LPAD(' ', 2 * LEVEL - 1) || ' ' || last_name AS employee
  3  FROM employees
  4  START WITH employee_id = 1004
  5  CONNECT BY PRIOR employee_id = manager_id;

     LEVEL     EMPLOYEE
------------------------------------------------------------------------------------
         1  Kyte
         2    Lawson
         2    Bliss


SQL>
SQL>
Home »
Oracle »
Select » 

Hierarchical Queries:
  1. Hierarchical Queries
  2. Formatting the Results from a Hierarchical Query
  3. Starting at a non-root Node using the START WITH clause.
  4. Using a Subquery in a START WITH Clause
  5. Traversing Upward Through the Tree
Related: