Employee Manager Report - Sql Server -


i have below table

empid   empname     managerid 1                   null 2          b           1 3          c           1 4          d           2 

the ddl under

declare @t table(empid int, empname varchar(20), managerid int) insert @t   select 1,'a',null union select 2,'b',1 union select 3,'c',1 union  select 4,'d',2 

what have prepare report indicate employees report manager.

i have soved using

select empname = e.empname, managername = m.empname @t e  left join @t m on  e.managerid = m.empid   

and desired output being

empname    managername          null b          c          d          b 

what other ways of doing so?

 declare @t table(empid int, empname varchar(20), managerid int)  insert @t  select 1,'a',null union select 2,'b',1 union select 3,'c',1 union  select 4,'d',2  ;with cte (     select empid,empname,managerid,     cast(empname varchar(max)) reportingmanager     @t     managerid null      union      select t.empid,t.empname,t.managerid,     cast(cte.empname+'->'+t.empname varchar(max)) reportingmanager     @t t     inner join cte on t.managerid=cte.empid  ) select * cte  

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -