避免使用嵌套选择插入插入选择中的Null

时间:2018-12-16 15:32:34

标签: sql-server

我正在尝试将数据从暂存DB加载到规范化DB中,但是一些数据在2018年尚未提供,因此我试图调整INSERT INTO SELECT查询以仅获取那些我的数据库中已经存在。

我希望能够插入我要添加的团队所在的Game表中。

暂存的数据如下:

ID     |  GameID        | Visitor  |  Event
____________________________________________
25301  |  BRO192104170  |  BSN     |  Strikeout
25302  |  BRO192104170  |  BSN     |  Hit
25303  |  BRO192104170  |  BSN     |  Homerun
25304  |  BRO192104170  |  BSN     |  Groundout
25305  |  BOS192104180  |  NY1     |  Groundout
25306  |  BOS192104180  |  NY1     |  Lineout

我目前具有以下Team表格:

ID   |  Team Name         |  RetrosheetID
___________________________________________
100  |  Boston Red Sox    |  BSN
101  |  Brooklyn Dodgers  |  BRO
102  |  New York Yankees  |  NY1

我想将暂存的数据加载到我的Game表中(显示了BRO与BSN):

ID   |  TeamID  |  DateID  |  OpponentID  |  GameNumberOfDay  |  RetroGameID
____________________________________________________________________________
100  |  101     |  10101   |  100         | 0                 | BRO192104170

当我需要过滤掉Team表中尚不存在的团队时,就会出现问题,因为该数据尚未可用。

以下是我当前的查询:

select distinct
    (select ID from Statistix.dbo.Team 
        where 
        (
            Year = (select SUBSTRING(GameID, 4, 4))
            AND
            retrosheet = (select SUBSTRING(GameID, 1, 3))
        )
    ) as TeamID,
    (select
        ID
    from Statistix.dbo.Date
        where
        (
            Year = (select SUBSTRING(GameID, 4, 4))
            AND
            Month = (select SUBSTRING(GameID,8,2))
            AND
            Day = (select SUBSTRING(GameID, 10, 2))
        )
    ) as DateID,
    (select ID from Statistix.dbo.Team 
        where 
        (
            Year = (select SUBSTRING(GameID, 4, 4))
            AND
            retrosheet = Visitor
        )
    ) as OpponentID,
    (select SUBSTRING(GameID, 12, 1)) as GameNumber,
    GameID
from dbo.Events e
where not exists (select * from Statistix.dbo.Game g where g.RetroGameID = GameID)

如果团队存在,它会起作用,所以我尝试将其添加到WHERE子句中:

AND (TeamID IS NOT NULL AND OpponentID IS NOT NULL)

但是,我收到此错误消息:

  

无效的列名“ TeamID”

     

无效的列名“ OpponentID”

1 个答案:

答案 0 :(得分:0)

这似乎有效:

select distinct
    (select ID from Statistix.dbo.Team 
        where 
        (
            Year = (select SUBSTRING(GameID, 4, 4))
            AND
            retrosheet = (select SUBSTRING(GameID, 1, 3))
        )
    ) as TeamID,
    (select
        ID
    from Statistix.dbo.Date
        where
        (
            Year = (select SUBSTRING(GameID, 4, 4))
            AND
            Month = (select SUBSTRING(GameID,8,2))
            AND
            Day = (select SUBSTRING(GameID, 10, 2))
        )
    ) as DateID,
    (select ID from Statistix.dbo.Team 
        where 
        (
            Year = (select SUBSTRING(GameID, 4, 4))
            AND
            retrosheet = Visitor
        )
    ) as OpponentID,
    (select SUBSTRING(GameID, 12, 1)) as GameNumber,
    GameID
from dbo.Events e
where not exists (select * from Statistix.dbo.Game g where g.RetroGameID = GameID)
and exists (select * from Statistix.dbo.Team t where t.retrosheet = (SELECT SUBSTRING(GameID, 1, 3)) and t.Year = (SELECT SUBSTRING(GameID, 4, 4)))
and exists (select * from Statistix.dbo.Team o where o.retrosheet = Visitor and o.Year = (SELECT SUBSTRING(GameID, 4, 4)))