同一列的多个 LEFT JOINS
Multiple LEFT JOINS for the same column
我在 SQL 中,我正在尝试使用多个左连接来填充单个列。我的第一个 LEFT JOIN 如下:
SELECT a.*, b.Game_Code
FROM [NCAAF].[dbo].[MasterSheet] a
LEFT JOIN (SELECT DISTINCT Game_Code, Date, Visit_Team_Code, Home_Team_Code
from dbo.game2018) b
ON (a.Date = b.Date and a.[Team Code] = b.Visit_Team_Code and a.[Opponent Code] = b.Home_Team_Code)
所有这一切都来之不易。它创建一个名为 Game_Code 的新列,并填充该列的 1/6,其余列为 NULL。对于其余的空值,我需要执行相同的 Left Join,但切换团队代码的地方除外。这个新的 Left Join 本身想要以下内容:
SELECT a.*, b.Game_Code
FROM [NCAAF].[dbo].[MasterSheet] a
LEFT JOIN (SELECT DISTINCT Game_Code, Date, Visit_Team_Code, Home_Team_Code
from dbo.game2018) b
ON (a.Date = b.Date and a.[Team Code] = b.Home_Team_Code and a.[Opponent Code] = b.Visit_Team_Code)
虽然我只想对剩余的 NULL 值执行此连接。当我尝试 运行 两个左连接在一起时,出现错误。我需要这样做的原因是因为每个游戏都列出了两次。一次使用客队代码,然后使用主队代码,然后再次使用主队代码,然后再使用客队代码。我知道这可能令人困惑,但我不想创建多个 Game_Code 列我只需要 1.Please 请参阅下面的示例: 两个表在左侧,所需结果在右侧。
First Left Join would satisfy the second row of the desired result and Second Left Join would satisfy the first row, but I can't figure out how to put them together
如果您 LEFT
加入同一个 table 两次,您可以只加入一次并在您的加入中添加一个 OR
并使用两个条件吗?
SELECT a.*, b.Game_Code
FROM [NCAAF].[dbo].[MasterSheet] a
LEFT JOIN (SELECT DISTINCT Game_Code, Date, Visit_Team_Code, Home_Team_Code
from dbo.game2018) b
ON a.Date = b.Date and
((a.[Team Code] = b.Visit_Team_Code and a.[Opponent Code] = b.Home_Team_Code)
OR
(a.[Team Code] = b.Home_Team_Code and a.[Opponent Code] = b.Visit_Team_Code))
看起来你只需要 INNER JOIN
而不需要 DISTINCT
:
select ms.date, ms.team_code, ms.opponent_code, g.game_code
from dbo.mastersheet ms
join dbo.game2018 g on g.date = ms.date
and g.home_team_code in (ms.team_code, ms.opponent_code)
and g.visit_team_code in (ms.team_code, ms.opponent_code)
order by ms.date, ms.team_code, ms.opponent_code;
我在 SQL 中,我正在尝试使用多个左连接来填充单个列。我的第一个 LEFT JOIN 如下:
SELECT a.*, b.Game_Code
FROM [NCAAF].[dbo].[MasterSheet] a
LEFT JOIN (SELECT DISTINCT Game_Code, Date, Visit_Team_Code, Home_Team_Code
from dbo.game2018) b
ON (a.Date = b.Date and a.[Team Code] = b.Visit_Team_Code and a.[Opponent Code] = b.Home_Team_Code)
所有这一切都来之不易。它创建一个名为 Game_Code 的新列,并填充该列的 1/6,其余列为 NULL。对于其余的空值,我需要执行相同的 Left Join,但切换团队代码的地方除外。这个新的 Left Join 本身想要以下内容:
SELECT a.*, b.Game_Code
FROM [NCAAF].[dbo].[MasterSheet] a
LEFT JOIN (SELECT DISTINCT Game_Code, Date, Visit_Team_Code, Home_Team_Code
from dbo.game2018) b
ON (a.Date = b.Date and a.[Team Code] = b.Home_Team_Code and a.[Opponent Code] = b.Visit_Team_Code)
虽然我只想对剩余的 NULL 值执行此连接。当我尝试 运行 两个左连接在一起时,出现错误。我需要这样做的原因是因为每个游戏都列出了两次。一次使用客队代码,然后使用主队代码,然后再次使用主队代码,然后再使用客队代码。我知道这可能令人困惑,但我不想创建多个 Game_Code 列我只需要 1.Please 请参阅下面的示例: 两个表在左侧,所需结果在右侧。 First Left Join would satisfy the second row of the desired result and Second Left Join would satisfy the first row, but I can't figure out how to put them together
如果您 LEFT
加入同一个 table 两次,您可以只加入一次并在您的加入中添加一个 OR
并使用两个条件吗?
SELECT a.*, b.Game_Code
FROM [NCAAF].[dbo].[MasterSheet] a
LEFT JOIN (SELECT DISTINCT Game_Code, Date, Visit_Team_Code, Home_Team_Code
from dbo.game2018) b
ON a.Date = b.Date and
((a.[Team Code] = b.Visit_Team_Code and a.[Opponent Code] = b.Home_Team_Code)
OR
(a.[Team Code] = b.Home_Team_Code and a.[Opponent Code] = b.Visit_Team_Code))
看起来你只需要 INNER JOIN
而不需要 DISTINCT
:
select ms.date, ms.team_code, ms.opponent_code, g.game_code
from dbo.mastersheet ms
join dbo.game2018 g on g.date = ms.date
and g.home_team_code in (ms.team_code, ms.opponent_code)
and g.visit_team_code in (ms.team_code, ms.opponent_code)
order by ms.date, ms.team_code, ms.opponent_code;