samedi 8 août 2015

Retrieving some specific records from SQL Server 2005 database table

I have a table in database

EmployeeID                TeamLeadID
2                          1
3                          2
4                          3
5                          NULL
1                          NULL
6                          1
7                          2
8                          3

Now what I want is to retrieve all the TeamLeadsID (i.e all the upper hierarchy) for a given EmployeeID

  • For EmployeeID = 2 I should get ans 1 (because we don't have TeamleadID for EmployeeId = 1)

  • For EmployeeID = 4 I should get 3,2 and 1 (because 4->Teamlead is 3, 3 -> Teamlead is 2, 2 -> TeamLead is 1)

  • Likewise for EmployeeID = 7 I should get 2 and 1 only

while for 5 & 1 it should be Null as clearly seen from the table itself

Aucun commentaire:

Enregistrer un commentaire