Member of the LearnItFirst.com Video Training Network | LearnSqlServer.com | SQL SSIS Training | SQL Programming Tutorials |
LearnSqlServer.com Forums LearnSqlServer.com
Welcome Guest Search | New Posts | Members | Log In | Register

Help with a CTE query Options
samcook1
Posted: Wednesday, July 02, 2008 9:17:54 AM
Rank: Newbie

Joined: 7/2/2008
Posts: 2
Points: 6
I really need this to display with persons under their direct manager, in more of a tree form, instead of listing each 'Tier' in order, any help would be greatly appreciated.

Here is the query (Results below):
Quote:
Declare @Manager varchar(100);
set @Manager = 'uid=jack.mccrossan@hp.com,ou=People,o=hp.com';

WITH EmployeeSubordinates (EmployeeID, LastName, FirstName, DistinguishedName, Mail, Manager,Tier) AS
(
SELECT
ID,
sn [LastName],
givenName [FirstName],
DistinguishedName,
Mail,
Manager,
0 AS Tier
FROM Employees WHERE
Employees.DistinguishedName = @Manager
UNION ALL
SELECT
e.ID,
e.sn [LastName],
e.givenName [FirstName],
e.DistinguishedName,
e.Mail,
e.Manager,
Mgr.Tier + 1 As Tier
FROM Employees e
INNER JOIN EmployeeSubordinates Mgr ON
e.Manager = Mgr.DistinguishedName
)

Select EmployeeID, LastName, FirstName, DistinguishedName, Mail, Tier, Manager from EmployeeSubordinates


Here are my results:

samcook1
Posted: Wednesday, July 02, 2008 1:26:12 PM
Rank: Newbie

Joined: 7/2/2008
Posts: 2
Points: 6
Just incase anyone was interested in seeing the complete solution, i had to use two loops to assign "groups" -- every manager and his subordinates were grouped together...

Quote:

drop table #MyTable;
Declare @Manager varchar(200);
Declare @TempManager varchar(200);
--set @Manager = 'uid=ann.livermore@hp.com,ou=People,o=hp.com';
set @Manager = 'uid=kim.box@hp.com,ou=People,o=hp.com';
--set @Manager = 'uid=jack.mccrossan@hp.com,ou=People,o=hp.com';
--set @Manager = 'uid=mark.hurd@hp.com,ou=People,o=hp.com';

WITH EmployeeSubordinates (EmployeeID, LastName, FirstName, DistinguishedName, Mail, Manager,Tier) AS
(
SELECT
ID,
sn [LastName],
givenName [FirstName],
DistinguishedName,
Mail,
Manager,
0 AS Tier
FROM Employees WHERE
Employees.DistinguishedName = @Manager
UNION ALL
SELECT
e.ID,
e.sn [LastName],
e.givenName [FirstName],
e.DistinguishedName,
e.Mail,
e.Manager,
Mgr.Tier + 1 As Tier
FROM Employees e
INNER JOIN EmployeeSubordinates Mgr ON
e.Manager = Mgr.DistinguishedName
)



Select EmployeeID, LastName, FirstName, DistinguishedName, Mail, Tier, Manager, 0 as [Group]
INTO #MyTable
from EmployeeSubordinates
OPTION (Maxrecursion 100)

Declare @Tier int
Set @Tier = 0
Declare @MgrID varchar(200)
Declare @group int
set @group = 0



WHILE (@Tier < 15)
BEGIN

DECLARE Mgrs CURSOR
For
Select Distinct Manager From #MyTable
where Tier = @Tier
Open Mgrs



Fetch Next FROM Mgrs INTO @MgrID

While (@@Fetch_Status = 0)
Begin

Update #MyTable Set [Group] = @group
where @MgrID = DistinguishedName
Update #MyTable Set [Group] = @group
where @MgrID = Manager

set @group = @group + 1

Fetch Next FROM Mgrs INTO @MgrID
END
close Mgrs
Deallocate Mgrs

set @Tier = @Tier + 1
END

select EmployeeID, LastName, FirstName, DistinguishedName, Mail, Tier, Manager, [Group] from #mytable
ORDER BY [Group],Tier
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.
     
Don't Forget!
LearnItFirst.com
Don't Forget!
LearnSqlServe.com
 
Home | About Us | Support | Contact Us | Privacy | Site Map | Blogs Blogs Refer a Friend and Get a Free Subscription!
© Copyright 2004-2007 LearnItFirst.com LLC. All rights reserved. All trademarks remain the property of their respective owners.
This site is not affiliated in any way with the Microsoft Corporation.