Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Dear Gurus,I have a VIEW like below:How do I query with given one EmpID to:1. Get all the above BossID?Example: Given EmpID = 12will return EmpID IN (01,03,09)?
If you are on SQL Server 2000 then read this: http://www.sqlteam.com/article/more-trees-hierarchies-in-sqlOn a higher version search for: "recursive CTE example" and I'm sure you will find a solution.No, you're never too old to Yak'n'Roll if you're too young to die.
calvinfoo
Posting Yak Master
129 Posts
Posted - 2012-01-05 : 05:51:08
Yes, this is the article that what my View is made and based onBut the article didn't answer my question... at least to my understanding...
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2012-01-05 : 09:44:51
its as simple as
;With CTEAS(SELECT EmpID, Name, BossID,CAST(1 AS int) AS [level]FROM TableWHERE EmpID = 12UNION ALLSELECT t.EmpID, t.Name,t.BossID,c.[Level] + 1FROM CTE cINNER JOIN Table tON t.EmpID = c.BossID)SELECT *FROM CTEWHERE [Level] >1
------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
calvinfoo
Posting Yak Master
129 Posts
Posted - 2012-01-05 : 20:26:50
Amazing.. I tried and it works! But the thing is, I still having hard time trying to digest how it works.... LOL...Anyway, Thanks!
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2012-01-06 : 13:40:37
quote:Originally posted by calvinfoo Amazing.. I tried and it works! But the thing is, I still having hard time trying to digest how it works.... LOL...Anyway, Thanks!