Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Hierarical query

$
0
0

Hi Gurus,

I have a employee table which contains empno, ename, supervisor number, supervisor name. Can you please assist me with the query like the data should be displayed in a tree structure. For Eg, if a any employee contains subordinates, the time it is displaying the hierarchy it should even display the subordinates information below the respective controlling employee.

i used the following query

WITH EMP ([EMPLOYEE_ID],[USER_NAME],  [SUPERVISOR_NAME]  ,[SUPERVISOR_NUMBER])
    AS
   (
    SELECT [EMPLOYEE_ID],[USER_NAME],[SUPERVISOR_NAME]  ,[SUPERVISOR_NUMBER]
    FROM Employee
    WHERE [SUPERVISOR_NUMBER] is not null
    UNION ALL
   
    SELECT a.[EMPLOYEE_ID], a.[USER_NAME], a.[SUPERVISOR_NAME]  ,a.[SUPERVISOR_NUMBER]
    FROM Employee as a inner
    JOIN EMP b ON a.[EMPLOYEE_ID]=b.[SUPERVISOR_NUMBER]
)
    SELECT EMPLOYEE_ID], [USER_NAME], [SUPERVISOR_NAME]  ,[SUPERVISOR_NUMBER]
    FROM EMP

But still it is not giving perfect order in tree structure.

Appreciate your quick help.

Thanks,

V.



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>