Retrieve records from a table.

  • 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 4

    2 bbb 3

    3 ccc 4

    4 ddd 2

    I 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 ManagerName

    aaa ddd

    bbb ccc

    ccc ddd

    ddd bbb

  • SElect a.EmpName EmployeeName, b.EmpName ManagerName from Employee a, Employee b

    where a.MngrID=b.EmpID

    Ryan
    //All our dreams can come true, if we have the courage to pursue them//

  • Hi

    You can have this way

    declare @Temp table (EmpId int, EmpName varchar(50), MngrID int)

    insert into @Temp

    Select 1, 'aaa', 4

    union Select 2, 'bbb', 3

    union Select 3, 'ccc', 4

    union Select 4, 'ddd', 2

    SElect a.EmpName EmployeeName, b.EmpName ManagerName from @Temp a, @Temp b

    where a.MngrID=b.EmpID

    or

    Select empname,(Select EmpName from @Temp as b where a.MngrID=b.empid ) as ManagerName from @Temp as a

    Thanks

    Parthi

    Thanks
    Parthi

  • Thank You. It worked:-)

  • Thank You Parthi..Its working...But I think this can be used when the table has less no of records.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply