在同一 table 上两次获取左连接的空数据
Getting null data for left join on same table twice
我有一些这样的表:
员工:
EmployeeId Name DeptId
----------------------------------
100 John 100
101 Kery 100
102 Tiny 100
团队:
TeamId TeamLeaderId TeamName
------------------------------------------
1 100 Finance-2021
团队成员:
TeamMemberId TeamId EmployeeId
----------------------------------------
1 1 101
我正在尝试获取员工名单以及有关他们是谁的信息:Team Member or Team Leader or Individual
。
当前输出:
TeamId TeamName EmployeeIdentity EmployeeId EmployeeName
------------------------------------------------------------------
null null Team Leader 100 John
1 Finance-2021 Team Member 101 Kery
预期输出:
TeamId TeamName EmployeeIdentity EmployeeId EmployeeName
-----------------------------------------------------------------------
1 Finance-2021 Team Leader 100 John
1 Finance-2021 Team Member 101 Kery
查询:
INSERT INTO #tempMembers (ID)
(SELECT EmployeeId
FROM TeamMembers
WHERE TeamId IN (1) AND IsDeleted = 0
UNION
SELECT TeamLeaderId
FROM TEAMS
WHERE TeamId IN (1))
SELECT
t.TeamId as 'TeamId',
t.TeamName as 'TeamName',
CASE
WHEN tm.EmployeeId IS NOT NULL THEN 'Team Member'
WHEN t1.TeamLeaderId IS NOT NULL THEN 'Team Leader'
ELSE 'Individual'
END AS 'EmployeeIdentity',
e.EmployeeId as 'EmployeeId',
e.EmployeeName as 'EmployeeName',
FROM
Employee AS e
LEFT JOIN
TeamMember tm ON tm.EmployeeId = e.EmployeeId AND tm.IsDeleted = 0
LEFT JOIN
Teams t ON t.TeamId = tm.TeamId
LEFT JOIN
Teams t1 ON t1.TeamLeaderId = e.EmployeeId
WHERE
e.IsDeleted = 0
AND e.EmployeeId IN (SELECT * FROM #tempMembers)
这里唯一的问题是我得到的“团队负责人”数据 TeamId and TeamName
为空白,如下所示:
当前输出:
TeamId TeamName EmployeeIdentity EmployeeId EmployeeName
null null Team Leader 100 John
谁能帮我解决这个问题?
这是一种方法:
select
t.teamid, t.teamName, 'Team Member' as EmployeeIdentity ,
e.employeeid , e.emplyeename
from
Employee e
join
TeamMember tm on tm.employeeid = e.employeeid
join
teams t on tm.TeamId = t.TeamId
union all
select
t.teamid, t.teamName, 'Team Leader' as EmployeeIdentity,
e.employeeid , e.emplyeename
from
employees e
join
Teams t on t.teamleaderid = e.employeeid
甚至更好:
select
t.teamid, t.teamName, EmployeeIdentity, e.employeeid, e.emplyeename
from
Employee e
join
(select
TeamId, Employeeid, 'Team Member' as EmployeeIdentity
from TeamMember
union all
select teamid, TeamLeaderId, 'Team Leader' as EmployeeIdentity
from team) as tm on tm.employeeid = e.employeeid
join
teams t on tm.TeamId = t.TeamId
我认为您的问题是您从 t 获取 teamid 和 teamname,而在领导案例中它来自 t1。试试这个:
Case When t.TeamId is null then t1.TeamId else t.TeamId end as 'TeamId',
Case When t.TeamName is null then t1.TeamName else t.TeamId end as 'TeamName',
此方法不需要临时 table 并使用左联接中的空值来确定成员是 Team Member
、Team Leader
还是 Individual
。
where 条件应用来自临时联合的过滤器 table。
我还在下面包含了一个 db-fiddle 来演示。
SELECT
CASE
WHEN tl.TeamLeaderId IS NOT NULL THEN tl.TeamId
WHEN tmt.TeamId IS NOT NULL THEN tmt.TeamId
ELSE NULL
END as TeamId,
CASE
WHEN tl.TeamLeaderId IS NOT NULL THEN tl.TeamName
WHEN tmt.TeamName IS NOT NULL THEN tmt.TeamName
ELSE NULL
END as TeamName,
CASE
WHEN tl.TeamLeaderId IS NOT NULL THEN 'Team Leader'
WHEN tm.EmployeeId IS NOT NULL THEN 'Team Member'
ELSE 'Individual'
END as EmployeeIdentity,
e.EmployeeId,
e.Name as EmployeeName
FROM
Employee e
LEFT JOIN
Teams tl ON e.EmployeeId = tl.TeamLeaderId AND tl.isDeleted=0
LEFT JOIN
TeamMember tm ON e.EmployeeId = tm.EmployeeId AND tm.isDeleted=0
LEFT JOIN
Teams tmt ON tm.TeamId = tmt.TeamId AND tmt.isDeleted=0
WHERE
tl.TeamId = 1 OR tmt.TeamId=1
TeamId
TeamName
EmployeeIdentity
EmployeeId
EmployeeName
1
Finance-2021
Team Leader
100
John
1
Finance-2021
Team Member
101
Kery
View on DB Fiddle or SQL Server Db Fiddle
让我知道这是否适合你。
您可以 select 团队领导和团队成员加入 APPLY
,然后加入 Employee
。不需要自连接或临时表
SELECT
t.TeamId,
t.TeamName,
m.EmployeeIdentity,
e.EmployeeId,
e.Name AS EmployeeName
FROM Teams t
CROSS APPLY (
SELECT
t.TeamLeaderId AS EmployeeId,
'Team Leader' AS EmployeeIdentity
UNION ALL
SELECT
tm.EmployeeId,
'Team Member'
FROM TeamMember tm
WHERE tm.TeamId = t.TeamId
) m
JOIN Employee e ON e.EmployeeId = m.EmployeeId
AND e.IsDeleted = 0
WHERE t.TeamId = 1;
我有一些这样的表:
员工:
EmployeeId Name DeptId
----------------------------------
100 John 100
101 Kery 100
102 Tiny 100
团队:
TeamId TeamLeaderId TeamName
------------------------------------------
1 100 Finance-2021
团队成员:
TeamMemberId TeamId EmployeeId
----------------------------------------
1 1 101
我正在尝试获取员工名单以及有关他们是谁的信息:Team Member or Team Leader or Individual
。
当前输出:
TeamId TeamName EmployeeIdentity EmployeeId EmployeeName
------------------------------------------------------------------
null null Team Leader 100 John
1 Finance-2021 Team Member 101 Kery
预期输出:
TeamId TeamName EmployeeIdentity EmployeeId EmployeeName
-----------------------------------------------------------------------
1 Finance-2021 Team Leader 100 John
1 Finance-2021 Team Member 101 Kery
查询:
INSERT INTO #tempMembers (ID)
(SELECT EmployeeId
FROM TeamMembers
WHERE TeamId IN (1) AND IsDeleted = 0
UNION
SELECT TeamLeaderId
FROM TEAMS
WHERE TeamId IN (1))
SELECT
t.TeamId as 'TeamId',
t.TeamName as 'TeamName',
CASE
WHEN tm.EmployeeId IS NOT NULL THEN 'Team Member'
WHEN t1.TeamLeaderId IS NOT NULL THEN 'Team Leader'
ELSE 'Individual'
END AS 'EmployeeIdentity',
e.EmployeeId as 'EmployeeId',
e.EmployeeName as 'EmployeeName',
FROM
Employee AS e
LEFT JOIN
TeamMember tm ON tm.EmployeeId = e.EmployeeId AND tm.IsDeleted = 0
LEFT JOIN
Teams t ON t.TeamId = tm.TeamId
LEFT JOIN
Teams t1 ON t1.TeamLeaderId = e.EmployeeId
WHERE
e.IsDeleted = 0
AND e.EmployeeId IN (SELECT * FROM #tempMembers)
这里唯一的问题是我得到的“团队负责人”数据 TeamId and TeamName
为空白,如下所示:
当前输出:
TeamId TeamName EmployeeIdentity EmployeeId EmployeeName
null null Team Leader 100 John
谁能帮我解决这个问题?
这是一种方法:
select
t.teamid, t.teamName, 'Team Member' as EmployeeIdentity ,
e.employeeid , e.emplyeename
from
Employee e
join
TeamMember tm on tm.employeeid = e.employeeid
join
teams t on tm.TeamId = t.TeamId
union all
select
t.teamid, t.teamName, 'Team Leader' as EmployeeIdentity,
e.employeeid , e.emplyeename
from
employees e
join
Teams t on t.teamleaderid = e.employeeid
甚至更好:
select
t.teamid, t.teamName, EmployeeIdentity, e.employeeid, e.emplyeename
from
Employee e
join
(select
TeamId, Employeeid, 'Team Member' as EmployeeIdentity
from TeamMember
union all
select teamid, TeamLeaderId, 'Team Leader' as EmployeeIdentity
from team) as tm on tm.employeeid = e.employeeid
join
teams t on tm.TeamId = t.TeamId
我认为您的问题是您从 t 获取 teamid 和 teamname,而在领导案例中它来自 t1。试试这个:
Case When t.TeamId is null then t1.TeamId else t.TeamId end as 'TeamId',
Case When t.TeamName is null then t1.TeamName else t.TeamId end as 'TeamName',
此方法不需要临时 table 并使用左联接中的空值来确定成员是 Team Member
、Team Leader
还是 Individual
。
where 条件应用来自临时联合的过滤器 table。
我还在下面包含了一个 db-fiddle 来演示。
SELECT
CASE
WHEN tl.TeamLeaderId IS NOT NULL THEN tl.TeamId
WHEN tmt.TeamId IS NOT NULL THEN tmt.TeamId
ELSE NULL
END as TeamId,
CASE
WHEN tl.TeamLeaderId IS NOT NULL THEN tl.TeamName
WHEN tmt.TeamName IS NOT NULL THEN tmt.TeamName
ELSE NULL
END as TeamName,
CASE
WHEN tl.TeamLeaderId IS NOT NULL THEN 'Team Leader'
WHEN tm.EmployeeId IS NOT NULL THEN 'Team Member'
ELSE 'Individual'
END as EmployeeIdentity,
e.EmployeeId,
e.Name as EmployeeName
FROM
Employee e
LEFT JOIN
Teams tl ON e.EmployeeId = tl.TeamLeaderId AND tl.isDeleted=0
LEFT JOIN
TeamMember tm ON e.EmployeeId = tm.EmployeeId AND tm.isDeleted=0
LEFT JOIN
Teams tmt ON tm.TeamId = tmt.TeamId AND tmt.isDeleted=0
WHERE
tl.TeamId = 1 OR tmt.TeamId=1
TeamId | TeamName | EmployeeIdentity | EmployeeId | EmployeeName |
---|---|---|---|---|
1 | Finance-2021 | Team Leader | 100 | John |
1 | Finance-2021 | Team Member | 101 | Kery |
View on DB Fiddle or SQL Server Db Fiddle
让我知道这是否适合你。
您可以 select 团队领导和团队成员加入 APPLY
,然后加入 Employee
。不需要自连接或临时表
SELECT
t.TeamId,
t.TeamName,
m.EmployeeIdentity,
e.EmployeeId,
e.Name AS EmployeeName
FROM Teams t
CROSS APPLY (
SELECT
t.TeamLeaderId AS EmployeeId,
'Team Leader' AS EmployeeIdentity
UNION ALL
SELECT
tm.EmployeeId,
'Team Member'
FROM TeamMember tm
WHERE tm.TeamId = t.TeamId
) m
JOIN Employee e ON e.EmployeeId = m.EmployeeId
AND e.IsDeleted = 0
WHERE t.TeamId = 1;