具有多左外连接的SQL Case语句

时间:2016-05-27 04:18:20

标签: sql join case

是否可以像我在下面那样做case语句和左外连接?我对我想要实现的目标发表评论。

SELECT
    eventGame.Id,
    eventGame.[Type] AS GameType
FROM GrassrootsHoops.EventGame eventGame
LEFT OUTER JOIN (SELECT
    divisionGameTeamResult.Id,
    divisionGameTeamResult.GameId,
    COALESCE(divisionPool.Name, divisionTeamPoolPoolBracket.Name) AS DivisionPoolName
FROM GrassrootsHoops.DivisionGameTeamResult divisionGameTeamResult
LEFT OUTER JOIN

-- CASE eventGame.[Type] = 1 THEN

GrassrootsHoops.DivisionTeamPoolGame divisionTeamPoolGame
    ON divisionTeamPoolGame.Id = divisionGameTeamResult.Id
LEFT OUTER JOIN GrassrootsHoopS.DivisionTeamPool divisionTeamPool
    ON divisionTeamPool.Id = divisionTeamPoolGame.DivisionTeamPoolId
LEFT OUTER JOIN GrassrootsHoops.DivisionPool divisionPool
    ON divisionPool.Id = divisionTeamPool.DivisionPoolId
LEFT OUTER JOIN GrassrootsHoops.DivisionPoolSettings divisionPoolSettings
    ON divisionPool.Id = divisionPoolSettings.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionTeam divisionTeam
    ON divisionTeam.Id = divisionTeamPool.DivisionTeamId
LEFT OUTER JOIN GrassrootsHoops.Team team
    ON team.Id = divisionTeam.Id
LEFT OUTER JOIN

-- CASE eventGame.[Type] = 2 THEN

GrassrootsHoops.DivisionBracketParticipant divisionBracketGameParticipant
    ON divisionBracketGameParticipant.DivisionGameTeamResultId = divisionGameTeamResult.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionBracketParticipantPart divisionBracketParticipantPart
    ON divisionBracketParticipantPart.Id = divisionBracketGameParticipant.DivisionBracketParticipantPartId
LEFT OUTER JOIN GrassrootsHoops.DivisionBracketParticipantTeam divisionBracketGameParticipantTeam
    ON divisionBracketGameParticipantTeam.Id = divisionBracketGameParticipant.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionTeam divisionTeamBracket
    ON divisionTeamBracket.Id = divisionBracketGameParticipantTeam.DivisionTeamId
LEFT OUTER JOIN GrassrootsHoops.Team teamBracket
    ON teamBracket.Id = divisionTeamBracket.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionBracketParticipantBracket divisionBracketParticipantBracket
    ON divisionBracketParticipantBracket.Id = divisionBracketGameParticipant.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionBracket divisionBracketParticipantBracketBracket
    ON divisionBracketParticipantBracketBracket.Id = divisionBracketParticipantBracket.DivisionBracketId
LEFT OUTER JOIN GrassrootsHoops.DivisionBracketParticipantPool divisionBracketGameParticipantPool
    ON divisionBracketGameParticipantPool.Id = divisionBracketGameParticipant.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionPool divisionPoolBracket
    ON divisionPoolBracket.Id = divisionBracketGameParticipantPool.DivisionPoolId
LEFT OUTER JOIN GrassrootsHoops.DivisionBracketParticipantTeamPool divisionBracketGameParticipantTeamPool
    ON divisionBracketGameParticipantTeamPool.Id = divisionBracketGameParticipant.Id
LEFT OUTER JOIN GrassrootsHoops.DivisionTeamPool divisionTeamPoolBracket
    ON divisionTeamPoolBracket.Id = divisionBracketGameParticipantTeamPool.DivisionTeamPoolId
LEFT OUTER JOIN GrassrootsHoops.DivisionPool divisionTeamPoolPoolBracket
    ON divisionTeamPoolPoolBracket.Id = divisionTeamPoolBracket.DivisionPoolId
LEFT OUTER JOIN GrassrootsHoops.DivisionTeam divisionTeamPoolTeamBracket
    ON divisionTeamPoolTeamBracket.Id = divisionTeamPoolBracket.DivisionTeamId
LEFT OUTER JOIN GrassrootsHoops.Team teamPoolTeamBracket
    ON teamPoolTeamBracket.Id = divisionTeamPoolTeamBracket.Id
WHERE divisionGameTeamResult.HomeTeam = 1) homeTeam
    ON homeTeam.GameId = eventGame.GameId

1 个答案:

答案 0 :(得分:1)

您不能使用外连接执行case语句。我认为你可以做你想要的,尽管使用UNION。这是一个使用更简单模式的示例,我们根据游戏类型进行两个单独的连接。

  SELECT
    Game.Id,
    PoolResult.Score
  FROM
    Game
  JOIN PoolResult
    ON PoolResult.GameId = Game.Id
  WHERE
    Game.GameType = 1
UNION
  SELECT
    Game.Id,
    BracketResult.Score
  FROM
    Game
  JOIN BracketResult
    ON BracketResult.GameId = Game.Id
  WHERE
    Game.GameType = 2

Here's a working version at SQL Fiddle

对于您的查询,您只想为每种类型的游戏编写两个完全独立的查询。一旦你为每个单独的查询工作,只需在它们之间插入一个UNION,结果将连接在一起。这是您的查询被修改为使用UNION。我没有一个简单的方法来检查它是否有错误,因此可能存在一些小错误

  SELECT
    divisionGameTeamResult.Id,
    eventGame.[Type] AS GameType
  FROM
    GrassrootsHoops.EventGame eventGame
  JOIN GrassrootsHoops.DivisionGameTeamResult divisionGameTeamResult
    ON (divisionGameTeamResult.GameId = eventGame.GameId
  JOIN GrassrootsHoops.DivisionTeamPoolGame divisionTeamPoolGame
    ON divisionTeamPoolGame.Id = divisionGameTeamResult.Id
  JOIN GrassrootsHoopS.DivisionTeamPool divisionTeamPool
    ON divisionTeamPool.Id = divisionTeamPoolGame.DivisionTeamPoolId
  JOIN GrassrootsHoops.DivisionPool divisionPool
    ON divisionPool.Id = divisionTeamPool.DivisionPoolId
  JOIN GrassrootsHoops.DivisionPoolSettings divisionPoolSettings
    ON divisionPool.Id = divisionPoolSettings.Id
  JOIN GrassrootsHoops.DivisionTeam divisionTeam
    ON divisionTeam.Id = divisionTeamPool.DivisionTeamId
  JOIN GrassrootsHoops.Team team
    ON team.Id = divisionTeam.Id
  WHERE
    divisionGameTeamResult.HomeTeam = 1
  AND
    eventGame.[Type] = 1
UNION
  SELECT
    divisionGameTeamResult.Id,
    eventGame.[Type] AS GameType
  FROM
    GrassrootsHoops.EventGame eventGame
  JOIN GrassrootsHoops.DivisionGameTeamResult divisionGameTeamResult
    ON (divisionGameTeamResult.GameId = eventGame.GameId)
  JOIN GrassrootsHoops.DivisionBracketParticipant divisionBracketGameParticipant
    ON divisionBracketGameParticipant.DivisionGameTeamResultId = divisionGameTeamResult.Id
  JOIN GrassrootsHoops.DivisionBracketParticipantPart divisionBracketParticipantPart
    ON divisionBracketParticipantPart.Id = divisionBracketGameParticipant.DivisionBracketParticipantPartId
  JOIN GrassrootsHoops.DivisionBracketParticipantTeam divisionBracketGameParticipantTeam
    ON divisionBracketGameParticipantTeam.Id = divisionBracketGameParticipant.Id
  JOIN GrassrootsHoops.DivisionTeam divisionTeamBracket
    ON divisionTeamBracket.Id = divisionBracketGameParticipantTeam.DivisionTeamId
  JOIN GrassrootsHoops.Team teamBracket
    ON teamBracket.Id = divisionTeamBracket.Id
  JOIN GrassrootsHoops.DivisionBracketParticipantBracket divisionBracketParticipantBracket
    ON divisionBracketParticipantBracket.Id = divisionBracketGameParticipant.Id
  JOIN GrassrootsHoops.DivisionBracket divisionBracketParticipantBracketBracket
    ON divisionBracketParticipantBracketBracket.Id = divisionBracketParticipantBracket.DivisionBracketId
  JOIN GrassrootsHoops.DivisionBracketParticipantPool divisionBracketGameParticipantPool
    ON divisionBracketGameParticipantPool.Id = divisionBracketGameParticipant.Id
  JOIN GrassrootsHoops.DivisionPool divisionPoolBracket
    ON divisionPoolBracket.Id = divisionBracketGameParticipantPool.DivisionPoolId
  JOIN GrassrootsHoops.DivisionBracketParticipantTeamPool divisionBracketGameParticipantTeamPool
    ON divisionBracketGameParticipantTeamPool.Id = divisionBracketGameParticipant.Id
  JOIN GrassrootsHoops.DivisionTeamPool divisionTeamPoolBracket
    ON divisionTeamPoolBracket.Id = divisionBracketGameParticipantTeamPool.DivisionTeamPoolId
  JOIN GrassrootsHoops.DivisionPool divisionTeamPoolPoolBracket
    ON divisionTeamPoolPoolBracket.Id = divisionTeamPoolBracket.DivisionPoolId
  JOIN GrassrootsHoops.DivisionTeam divisionTeamPoolTeamBracket
    ON divisionTeamPoolTeamBracket.Id = divisionTeamPoolBracket.DivisionTeamId
  JOIN GrassrootsHoops.Team teamPoolTeamBracket
    ON teamPoolTeamBracket.Id = divisionTeamPoolTeamBracket.Id
  WHERE
    divisionGameTeamResult.HomeTeam = 1
  AND
    eventGame.[Type] = 1