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 |
|
chitturiii
Starting Member
2 Posts |
Posted - 2010-11-09 : 06:45:09
|
| Hi All,Please help me with the below pblm.Suppose there is a table 'Employee' with columns EmpID,EmpName,ManagerID and with records as below:EmpId EmpName MngrID 1 aaa 42 bbb 33 ccc 44 ddd 2I want a query to get the EmpName and ManagerName as resultset from the Employee table. (Here Manager is again an employee in the company and so if Mngrid is 4 then his name is ddd) .So finally my result should be:EmpName ManagerNameaaa dddbbb cccccc dddddd bbb |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-11-09 : 06:51:49
|
| I feel its a homework.Hint for you to start with.Join the same table two times with different alias and the joining link will be EmpID of one table with MgrID of next table.Try and frame the sql and post the sql if you face any problem. |
 |
|
|
chitturiii
Starting Member
2 Posts |
Posted - 2010-11-09 : 07:16:52
|
Thank You for the hint :)The below query works:Select a.EmpName EmployeeName, b.EmpName ManagerName from Employee a, Employee bwhere a.MngrID=b.EmpID quote: Originally posted by pk_bohra I feel its a homework.Hint for you to start with.Join the same table two times with different alias and the joining link will be EmpID of one table with MgrID of next table.Try and frame the sql and post the sql if you face any problem.
|
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-11-09 : 07:30:12
|
Better use the JOIN syntax:SELECT a.EmpName AS EmployeeName, b.EmpName AS ManagerName FROM Employee AS a INNER JOIN Employee AS b ON a.MngrID=b.EmpID No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-11-09 : 08:50:15
|
quote: Originally posted by webfred Better use the ANSI JOIN syntax:SELECT a.EmpName AS EmployeeName, b.EmpName AS ManagerName FROM Employee AS a INNER JOIN Employee AS b ON a.MngrID=b.EmpID No, you're never too old to Yak'n'Roll if you're too young to die.
MadhivananFailing to plan is Planning to fail |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-11-09 : 09:02:10
|
Yes yes yes  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|