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 |
|
arun_1328
Starting Member
2 Posts |
Posted - 2009-06-15 : 03:18:54
|
Consider the following table structure of Employees reporting to their managers in a particular companyEmployeeId ManagerId1 NULL2 13 14 35 26 27 6Now I want to display the employees who report a particular employee say "2" and their corresponding level with respect to that employee. The output similar to thisEmployeeId Level ManagerId 5 1 26 1 27 2 6 Can anyone suggest a solution on how to achieve this using WITH Clause query. Thank you.Arun Vijay.V |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-06-15 : 03:56:58
|
[code]DECLARE @Sample TABLE ( empID INT, mgrID INT )INSERT @SampleSELECT 1, NULL UNION ALLSELECT 2, 1 UNION ALLSELECT 3, 1 UNION ALLSELECT 4, 3 UNION ALLSELECT 5, 2 UNION ALLSELECT 6, 2 UNION ALLSELECT 7, 6;WITH Yak (empID, mgrID, empPath)AS ( SELECT empID, mgrID, '/' + CAST(empID AS VARCHAR(MAX)) + '/' FROM @Sample WHERE mgrID IS NULL UNION ALL SELECT s.empID, s.mgrID, y.empPath + CAST(s.empID AS VARCHAR(MAX)) + '/' FROM @Sample AS s INNER JOIN Yak AS y ON y.empID = s.mgrID)SELECT empID AS EmployeeID, LEN(empPath) - LEN(REPLACE(empPath, '/', '')) - 3 AS [Level], mgrID AS ManagerIDFROM YakWHERE empPath LIKE '%/2/%' AND empID <> 2ORDER BY empID[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
malaytech2008
Yak Posting Veteran
95 Posts |
Posted - 2009-06-15 : 03:58:52
|
| I have give an idea you can change it---declare @eid intdeclare @level intdeclare @count intcreate table emp(eid int,levels int,mgrid int) declare ctest cursor for select eid from testbeginopen ctestfetch ctest into @eidwhile @@FETCH_STATUS=0 begin declare @mgr int select @mgr=mgrid from test where eid=@eid --write inner cursor to find the lvel if @mgr is null begin set @count=1 end else begin set @count=1 while (@mgr is not null) begin select @mgr=mgrid from test where eid=@mgr set @count=@count+1 continue end endinsert into emp select eid,@count as levels,mgrid from test where eid=@eidfetch ctest into @eidendclose ctestdeallocate ctestendselect * from empdrop table empMalay |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-06-15 : 04:23:52
|
quote: Originally posted by malaytech2008 I have give an idea you can change it---declare @eid intdeclare @level intdeclare @count intcreate table emp(eid int,levels int,mgrid int) declare ctest cursor for select eid from testbeginopen ctestfetch ctest into @eidwhile @@FETCH_STATUS=0 begin declare @mgr int select @mgr=mgrid from test where eid=@eid --write inner cursor to find the lvel if @mgr is null begin set @count=1 end else begin set @count=1 while (@mgr is not null) begin select @mgr=mgrid from test where eid=@mgr set @count=@count+1 continue end endinsert into emp select eid,@count as levels,mgrid from test where eid=@eidfetch ctest into @eidendclose ctestdeallocate ctestendselect * from empdrop table empMalay
Do not use cursor for this No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-06-15 : 04:58:35
|
Why not?Some people like to shoot themself in their feet. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-06-15 : 05:03:43
|
quote: Originally posted by Peso Why not?Some people like to shoot themself in their feet. E 12°55'05.63"N 56°04'39.26"
And why they are using a squirt gun then? No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-06-15 : 05:04:39
|
Ooohh.. a CURSOR would be the equivalent of Big Bertha. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
arun_1328
Starting Member
2 Posts |
Posted - 2009-06-15 : 07:40:14
|
| Thank you for your repliesArun Vijay.V |
 |
|
|
|
|
|
|
|