Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to modify the query to get the required resultset?

Status
Not open for further replies.
Oct 11, 2006
300
US
Using this query, I got the indentation of everybody in the orgstructure. I just showed the data for one top level manager - W. McMonigal so that you can see the indentation for him and his subordinated who have the indentation of 5.

The query I made to get this indented Org Structure is:

Code:
SELECT  COUNT(s2.employee) AS indentation, s1.employee, s1.Lft, s1.Rgt, dbo.Employee.FirstName + ' ' + dbo.Employee.LastName AS Emp_Name, dbo.Employee.LastName, dbo.Employee.EmpID
FROM  dbo.Employee INNER JOIN
dbo.Stack1 s1 ON dbo.Employee.UniqueIdentifier = s1.employee CROSS JOIN
dbo.Stack1 s2
WHERE     (s1.Lft BETWEEN s2.Lft AND s2.Rgt)
GROUP BY s1.employee, s1.Lft, dbo.Employee.DisplayName, s1.Rgt, dbo.Employee.FirstName + ' ' + dbo.Employee.LastName, dbo.Employee.LastName, 
dbo.Employee.EmpID

Result set for the above query is:

Code:
indentation employee Lft  Rgt Emp_Name          EmpID
4	    862336   604  617 William McMonigal	41495
5	    627298   605  606 David Block	41771
5	    862337   607  608 Joseph Hennessy	41475
5           862339   609  610 Jason Pitre       41503
5           862362   611  612 George Seth       41502
5           862363   613  614 John Spartz       41558
5           2356682  615  616 Tim Krug          41612

However I would like to know the subordinates for W.Mcmonigal when I pass his employee number 862336.

So I tried this:

Code:
SELECT  E.FirstName + ' ' + E.LastName AS Emp_Name, s3.Indentation, s2.employee AS MgrID, s1.employee AS SubID, s1.Lft, s1.Rgt
FROM dbo.Stack1 s1 CROSS JOIN
dbo.Stack1 s2 INNER JOIN
dbo.Employee E ON s1.employee = E.UniqueIdentifier INNER JOIN
(SELECT COUNT(s2.employee) AS Indentation, s1.employee
FROM dbo.Stack1 s1 CROSS JOIN
dbo.Stack1 s2
WHERE (s1.Lft BETWEEN s2.Lft AND s2.Rgt)
GROUP BY s1.Lft, s1.employee) s3 
ON s2.employee = s3.employee WHERE
(s1.Lft BETWEEN s2.Lft AND s2.Rgt) 
AND (s2.employee = '862336')

However the result looks like this:

Code:
Emp_Name         Indent Mgr_ID  Sub_ID    Lft   Rgt

William McMonigal  4    862336  862336    604   617
David Block        4    862336  627298    605   606
Joseph Hennessy	   4    862336  862337    607   608
Jason Pitre        4    862336  862339    609   610
George Seth        4    862336  862362    611   612
John Spartz        4    862336  862363    613   614
Tim Krug           4    862336  2356682   615   616

Can you please suggest how can I get the indentation of the subordinates of W.Mcmonigal to show as 4 and W.Mcmonigal to show as 5 as in the previous result list.

Thanks.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top