Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Recursive Query

Author  Topic 

bobbyjha555
Starting Member

1 Post

Posted - 2014-01-05 : 18:45:54

Hi,

I have a table which has 2 columns and many rows (below is a example : original table has around ~2k lines in it ).

2 columns are

user manager

EG :

john peter
joe peter
sam peter
jill boyd
rav boyd
venkat raj
surku raj
domc joe
hans joe
jeen rav
pam surku
harry domc

I need to have a script which can give me output which will have directreport till 2 level deep ( Ignore anything which is more than 2 levels deep )

manager, directreport, directreport1, directreport2, directmanager1, directmanager2

so for abovve the output should be

peter john null null null null
peter joe null null null null
peter sam null null null null
peter null domc null joe null
peter null hans null joe null
peter null null harry null domc
boyd jill null null null null
boyd rav null null null null
boyd null jeen null rav null
raj venkat null null null null
raj surku null null null null
joe domc null null null null
joe hans null null null null
joe null harry null domc null
rav jean null null null null
surku pam null null null null
domc harry null null null null


Regards

Bobby

BobbyJha

lohumi
Starting Member

6 Posts

Posted - 2014-01-06 : 04:01:46
you might get something useful here.
http://blog.sqlauthority.com/2012/04/24/sql-server-introduction-to-hierarchical-query-using-a-recursive-cte-a-primer/

Lohumi
Go to Top of Page
   

- Advertisement -