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.
Author |
Topic |
IK1972
56 Posts |
Posted - 2012-09-10 : 20:05:54
|
I have one table with 4 filedsEmpId, ParentID, Name, Titlewe have hierarchy data in this table. I want to get a Path for every user.Like ID - Name - Path1 - a - /2 - b - /a3 - c - /b/aThanks |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-10 : 20:18:02
|
[code];With Emp_Hierarchy AS(SELECT EmpId,ParentID,Name, CAST('/' AS varchar(max)) AS [Path]FROM tableWHERE ParentID IS NULLUNION ALLSELECT t.EmpId,t.ParentID,t.Name,e.[Path] + e.[Name]FROM Emp_Hierarchy eINNER JOIN table tON t.ParentID = e.EmpId )SELECT EmpID AS ID,[Name],[Path]FROM Emp_Hierarchy [/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
IK1972
56 Posts |
Posted - 2012-09-11 : 16:24:48
|
Thanks |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-11 : 16:26:00
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
IK1972
56 Posts |
Posted - 2012-09-11 : 23:27:52
|
I try this and I saw this is from Top to Bottom. Do you how I can get a path from Bottom to Top. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-11 : 23:57:56
|
[code];With Emp_Hierarchy AS(SELECT EmpId,ParentID,Name, CAST('/' AS varchar(max)) AS [Path]FROM table t1LEFT JOIN table t2ON t2.ParentID = t1.EmpId WHERE t2.EmpID IS NULLUNION ALLSELECT t.EmpId,t.ParentID,t.Name,e.[Path] + e.[Name]FROM Emp_Hierarchy eINNER JOIN table tON t.EmpID= e.ParentID )SELECT EmpID AS ID,[Name],[Path]FROM Emp_Hierarchy [/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
IK1972
56 Posts |
Posted - 2012-09-12 : 14:09:02
|
No its not giveing expected result from Boton to Top. I saw you mentioend EMPID is NULL in first part. I dont think so I have mull in EMPID. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-12 : 14:17:13
|
see LEFT JOIN usage. that logic was to identify the child records (bottom most).------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-12 : 14:43:26
|
show us what your intended result is out of a sample data.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
IK1972
56 Posts |
Posted - 2012-09-12 : 15:28:47
|
create table #emp(ID int, ParentID int, Name varchar(20))insert into #emp values(0, null, 'a'),(1,0,'b'),(2,0,'c'),(3,1,'d'),(4,3,'e'),(5,4,'f')select * from #empFirst expected result as you can see its start from user and end till root. -- expect resultID, Name, Path1, b, a2, c, a3, d, d/b/a4, e, e/d/b/a5 f, f/e/d/b/asecond expected result is its start from user and end before root level.-- expect resultID, Name, Path1, b, 2, c, 3, d, d/b/4, e, e/d/b/5 f, f/e/d/b/Thanks |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-12 : 15:45:05
|
your path value is not consistentwhy does record for b doesnt have itself included in path (value is just a) whereas in next level you've value for d as d/b/a------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
IK1972
56 Posts |
Posted - 2012-09-12 : 16:40:02
|
Yes this is type mistake. even for b if I get b/a its fine. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-12 : 17:16:42
|
[code];With Emp_Hierarchy AS(SELECT t1.ID,t1.ParentID,t1.Name, CAST(t1.Name AS varchar(max)) AS [Path]FROM #emp t1WHERE t1.ParentID IS NULLUNION ALLSELECT t.ID,t.ParentID,t.Name,CAST(t.[name] + '/' + e.[path] AS varchar(max))FROM Emp_Hierarchy eINNER JOIN #emp tON t.ParentID= e.ID )SELECT ID,[Name],[Path]FROM Emp_Hierarchy output---------------------0 a a1 b b/a2 c c/a3 d d/b/a4 e e/d/b/a5 f f/e/d/b/a[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|