Heirarchy query - Need help please!

DB ver: Oracle DB 10g Rel2
I have data as follows
LAST_NAME EMPLOYEE_ID MANAGER_ID
King 100 null
Cambrault 148 100
Bates 172 148
Bloom 169 148
Fox 170 148
Ozer 168 172
Smith 171 172
De Haan 102 169
Hunold 103 169
Austin 105 169
Ernst 104 170
When Cambrault(emp.id:148) logs in he should see the reporting structure as follows
- Cambrault
-- Bates
--- Ozer
--- Smith
-- Bloom
--- De Haan
--- Hunold
--- Austin
-- Fox
--- Ernst
Similerly when Bates(Emp.Id:172) logs in he should see as follows
- Bates
-- Ozer
-- Smith
How to write a query to get output of records as above?
I could write it as follows but getting all the records.
SELECT e1.ename||' works for Supervisor '||e2.ename
FROM emp e1, emp e2
WHERE e1.mgr = e2.empno;
Any help is highly appreciated. Thanks in advance.
movva
Edited by: cmovva on 27-Oct-2011 12:07 PM

maybe this example might help.
SQL> select * from emp;
EMPNO ENAME      JOB         MGR HIREDATE          SAL      COMM DEPTNO
7902 FORD       ANALYST    7782 03-Dec-81     3000.00               20
7839 KING       PRESIDENT       17-Nov-81     5000.00               10
7698 BLAKE      MANAGER    7839 01-May-81     2850.00               30
7782 CLARK      MANAGER    7839 09-Jun-81     2450.00               10
7788 SCOTT      ANALYST    7782 09-Dec-82     3000.00               20
7844 TURNER     SALESMAN   7698 08-Sep-81     1500.00      0.00     30
7876 ADAMS      CLERK      7782 12-Jan-83     1100.00               20
7900 JAMES      CLERK      7698 03-Dec-81      950.00               30
7934 MILLER     CLERK      7782 23-Jan-82     1300.00               10
7945 CINDY      SALESMAN   7698 16-Jan-83     1800.00               30
7950 TINA       SALESMAN   7698 18-Jan-83     1850.00               30
11 rows selected
SQL>
SQL>  select substr(lpad(' ',2*(level-1)) || ename,1,40) employee_name, job, hiredate, sal
  2    from emp
  3    start with mgr is null
  4    connect by prior empno = mgr;
EMPLOYEE_NAME                            JOB       HIREDATE          SAL
KING                                     PRESIDENT 17-Nov-81     5000.00
  BLAKE                                  MANAGER   01-May-81     2850.00
    TURNER                               SALESMAN  08-Sep-81     1500.00
    JAMES                                CLERK     03-Dec-81      950.00
    CINDY                                SALESMAN  16-Jan-83     1800.00
    TINA                                 SALESMAN  18-Jan-83     1850.00
  CLARK                                  MANAGER   09-Jun-81     2450.00
    SCOTT                                ANALYST   09-Dec-82     3000.00
    ADAMS                                CLERK     12-Jan-83     1100.00
    FORD                                 ANALYST   03-Dec-81     3000.00
    MILLER                               CLERK     23-Jan-82     1300.00
11 rows selected
SQL> using your sample data something like this:
SQL> select *
  2    from (select 'King'      last_name, 100 employee_id, null manager_id from dual union all
  3          select 'Cambrault' last_name, 148 employee_id, 100  manager_id from dual union all
  4          select 'Bates'     last_name, 172 employee_id, 148  manager_id from dual union all
  5          select 'Bloom'     last_name, 169 employee_id, 148  manager_id from dual union all
  6          select 'Fox'       last_name, 170 employee_id, 148  manager_id from dual union all
  7          select 'Ozer'      last_name, 168 employee_id, 172  manager_id from dual union all
  8          select 'Smith'     last_name, 171 employee_id, 172  manager_id from dual union all
  9          select 'De,Haan'   last_name, 102 employee_id, 169  manager_id from dual union all
10          select 'Hunold'    last_name, 103 employee_id, 169  manager_id from dual union all
11          select 'Austin'    last_name, 105 employee_id, 169  manager_id from dual union all
12          select 'Ernst'     last_name, 104 employee_id, 170  manager_id from dual) e;
LAST_NAME EMPLOYEE_ID MANAGER_ID
King              100
Cambrault         148        100
Bates             172        148
Bloom             169        148
Fox               170        148
Ozer              168        172
Smith             171        172
De,Haan           102        169
Hunold            103        169
Austin            105        169
Ernst             104        170
11 rows selected
SQL>
SQL> select substr(lpad(' ',2*(level-1)) || e.last_name,1,40) last_name,
  2         e.employee_id,
  3         e.manager_id
  4    from (select 'King'      last_name, 100 employee_id, null manager_id from dual union all
  5          select 'Cambrault' last_name, 148 employee_id, 100  manager_id from dual union all
  6          select 'Bates'     last_name, 172 employee_id, 148  manager_id from dual union all
  7          select 'Bloom'     last_name, 169 employee_id, 148  manager_id from dual union all
  8          select 'Fox'       last_name, 170 employee_id, 148  manager_id from dual union all
  9          select 'Ozer'      last_name, 168 employee_id, 172  manager_id from dual union all
10          select 'Smith'     last_name, 171 employee_id, 172  manager_id from dual union all
11          select 'De,Haan'   last_name, 102 employee_id, 169  manager_id from dual union all
12          select 'Hunold'    last_name, 103 employee_id, 169  manager_id from dual union all
13          select 'Austin'    last_name, 105 employee_id, 169  manager_id from dual union all
14          select 'Ernst'     last_name, 104 employee_id, 170  manager_id from dual) e
15    start with e.manager_id is null
16    connect by prior e.employee_id = e.manager_id;
LAST_NAME                                EMPLOYEE_ID MANAGER_ID
King                                             100
  Cambrault                                      148        100
    Bloom                                        169        148
      De,Haan                                    102        169
      Hunold                                     103        169
      Austin                                     105        169
    Fox                                          170        148
      Ernst                                      104        170
    Bates                                        172        148
      Ozer                                       168        172
      Smith                                      171        172
11 rows selected
SQL>

Similar Messages

Maybe you are looking for

  • Table fields to match pdf form

    I found this on my formscentral help file. Note: Some types of form fields aren't supported when you import a PDF form. For example, text fields with formatting applied, such as numeric or percentage fields, are converted to plain text fields. And, s

  • How to override setText for custom JButton

    Hi From the Swings Hack book, I have the code for a custom JButton. I would like to setText for this button so that the text is set over the image on the button. Would someone know how I can do this? import java.awt.*; import javax.swing.*; public cl

  • Re-name worksheet tabs from cell contents?

    Is is possible to re-name worksheet tabs automatically from cell contents in another sheet within the same workbook? I have a workbook with 30 sheets all feeding data into sheet 31 - the Master Worksheet where all the data is collated etc. There are

  • Receiver JMS Configuration

    Hi Friends, I am configuring the receiver JMS adapter- 1.In the JMS Settings section there is a option called "JMS ReplyTo Queue Name". I am not sure about the significance of it. 2.Also let me know whether it is mandatory to fill? I have seen few bl

  • Did CS4's multiple artboards kill any crop area capabilities?

    I'm still gobsmacked by the multiple artboard stuff... but I see my favorite CS3 feature, multiple crop areas is gone (or at least appears to be gone). I came to depend rather heavily on multiple crop areas -- so I'm anxious that some facet of them c