如何连接同一表中的2列

问题描述

我在表“ userID”和“ manager”中有2列。每个userID都有一个管理员,并且每个管理员都在userID列中列出,并且每个管理员都有一个管理员

我如何创建一个名为“ skip_manager”的新列,并为每个用户ID的管理员找到他的管理员

Select
    employee_id as ID,login_name as userID,employee_name as Name,department_id as BU,supervisor_login_name as manager,business_title as title,hire_date,is_active_record,effective_start_day,effective_end_day,is_employed,job_title

From 
BOOKER.D_EMPLOYEE_HISTORY

Where
department_id in (5404,5406,5405,5412,5452,4345,4366) 

我需要添加一个名为skip_manager的列,以便在该列管理器附近导出管理器的管理器。

出口前:

id        userid    name    bu  manager
1031    xxxxche Lxxxxxan 5406   sxxxxb
1032    xxxxarc Bxxxxxxan 5406  paxxxxxa
1006    xxxxpem Mxxxxxru 5406   ixxxxar

解决方法

选中此项-

create table EMP_TABLE
(
    UserId int,ManagerId int,FirstName varchar(20)
)

insert into EMP_TABLE
select 1,2,'Ajit' union
select 2,3,'Amar' union
select 3,4,'Kiran' union
select 4,1,'Vishal' union
select 5,'Ram' 

select * from EMP_TABLE

SELECT T1.*,(select ManagerId from EMP_TABLE c where T1.ManagerId = c.UserId) 'skip_manager_Id'
FROM EMP_TABLE T1
JOIN EMP_TABLE T2 ON T2.userID = T1.ManagerId
,

此技术称为自连接。您可以加入同一张表的2个实例。

SELECT *
FROM EMP_TABLE T1
JOIN EMP_TABLE T2 ON T2.userID = T1.manager
<WHERE CONDITION>