apkos99
apkos99

Reputation: 109

sql query create new column and add data

enter image description here

I want to retrieve the employee name with the name of their manager.

For example:

Employee_Lastname: WARX
Employee_Firstname : CYNTHIA
MANAGER_NANE: SMITH

Warx Cythnia has tha manager with Manager_ID=7369 who is Smith John because Smith has the Employee_ID=7369

I want to create the MANGER_NAME column and add data...

Upvotes: 0

Views: 2338

Answers (2)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239814

Normally, you wouldn't store such information. But you can easily obtain it when querying:

select
   t1.Employee_Lastname,
   t1.Employee_Firstname,
   t2.Employee_Lastname as manager_name
from [table] t1
       left join
     [table] t2
        on
           t1.manager_id = t2.employee_id

Upvotes: 1

John Woo
John Woo

Reputation: 263883

maybe you only want to project the value of the record and not by adding new column in your table right? you only need to join this,

SELECT  a.*,
        b.Employee_LastName AS MANAGER_LASTNAME
FROM    EmpTable a
        LEFT JOIN EmpTable b
            ON a.Manager_ID = b.Employee_ID

or

SELECT  a.Employee_LastName,
        a.Employee_FirstName,
        b.Employee_LastName AS MANAGER_LASTNAME
FROM    EmpTable a
        LEFT JOIN EmpTable b
            ON a.Manager_ID = b.Employee_ID

Upvotes: 1

Related Questions