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 |
|
kunal1982
Starting Member
3 Posts |
Posted - 2009-01-22 : 10:12:07
|
| I have a table in which i have Employee Manager relationship (Self Referencing) For EgEmp_ID Name ManagerID001 E1 002002 E2 003003 E3 004004 E4 NULL005 E5 006006 E6 NULLNow i want to construct a view which shows Root (Parent manager of each employee) not just immediate manager. Actually Root is a department head of the department.So each employee row should have a column for their respective Department HeadLike Emp_ID Name Department Head001 E1 004002 E2 004003 E3 004004 E4 NULL005 E5 006006 E6 NULLAny Idea how to go about this |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-22 : 10:18:29
|
| look at cte concept |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-22 : 11:11:01
|
| http://msdn.microsoft.com/en-us/library/ms186243.aspx |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-22 : 11:21:49
|
| [code];With CTE(Emp_ID ,Name ,ManagerID)AS(SELECT Emp_ID, Name, COALESCE(CAST(ManagerID AS varchar(max))+ ',','')FROM TableWHERE ManagerID IS NOT NULLUNION ALLSELECT t.Emp_ID, t.Name,COALESCE(CAST(t.ManagerID AS varchar(max))+',','')+ c.ManagerIDFROM CTE cINNER JOIN Table tON t.Emp_ID=c.ManagerID)SELECT Emp_ID ,Name ,LEFT(ManagerID,CHARINDEX(',',MangerID)-1) AS [Department Head]FROM CTE[/code] |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-22 : 12:14:21
|
quote: Originally posted by visakh16
;With CTE(Emp_ID ,Name ,ManagerID)AS(SELECT Emp_ID, Name, COALESCE(CAST(ManagerID AS varchar(max))+ ',','')FROM TableWHERE ManagerID IS NOT NULLUNION ALLSELECT t.Emp_ID, t.Name,COALESCE(CAST(t.ManagerID AS varchar(max))+',','')+ c.ManagerIDFROM CTE cINNER JOIN Table tON t.Emp_ID=c.ManagerID)SELECT Emp_ID ,Name ,LEFT(ManagerID,CHARINDEX(',',MangerID)-1) AS [Department Head]FROM CTE
Hi visakh, your cte is not giving what op wantsActually op wants asEmp_ID Name Department Head001 E1 004002 E2 004003 E3 004004 E4 NULL005 E5 006006 E6 NULLwhich is mentioned earlier but your cte is giving o/p asemp_id name department Head001 E1 002003 E3 004005 E5 006002 E2 003where for emp_ids 001,002,003 should have departmenthead as 004 like that... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-22 : 12:21:04
|
| ok...will test and post new solution tomm |
 |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2009-01-23 : 02:37:44
|
| [code]Hi Try thisscreate function dbo.udf_cte( @empid int)returns @Table table (i int)ASbegin with cte(empid,name,managerid) as ( select empid,name,managerid from urtable where empid = @empid union all select t.empid,t.name,t.managerid from urtable t inner join cte on (cte.managerid = t.empid) ) insert into @Table select empid from cte where managerid is null returnendselect a.empid,a.name,NULLIF(b.i,a.empid) as managerid from urtable across apply( select * from dbo.udf_cte(a.empid)) bJai Krishna[/code] |
 |
|
|
PeterNeo
Constraint Violating Yak Guru
357 Posts |
Posted - 2009-01-23 : 07:21:05
|
try like thisDECLARE @T TABLE ( Emp_ID VARCHAR(10), [Name] VARCHAR(10), ManagerID VARCHAR(10))INSERT INTO @T SELECT '001', 'E1', '002'UNION ALL SELECT '002', 'E2', '003'UNION ALL SELECT '003', 'E3', '004'UNION ALL SELECT '004', 'E4', NULLUNION ALL SELECT '005', 'E5', '006'UNION ALL SELECT '006', 'E6', NULL; WITH Cte ( Emp_ID, [Name], ManagerId, [Path] )AS( SELECT Emp_ID, [Name], ManagerId, CONVERT(VARCHAR(MAX), Emp_ID) FROM @T WHERE ManagerID IS NULL UNION ALL SELECT t.Emp_ID,t.[Name],t.ManagerId, cte.[Path] + '-' + t.Emp_ID FROM @T t INNER JOIN Cte Cte ON (t.ManagerId = cte.Emp_ID) )UPDATE TSET ManagerId = LEFT(C.path, 3)FROM @T TINNER JOIN Cte C ON LEFT(C.path, 3) <> T.Emp_Id AND C.Path + '-' LIKE '%-' + T.Emp_Id + '-%'SELECT * FROM @T "There is only one difference between a dream and an aim.A dream requires soundless sleep to see,whereas an aim requires sleepless efforts to achieve..!!" |
 |
|
|
|
|
|
|
|