2

我有两个表,我想加入以下列:

表:#IndexDecomposition;组分。

行:标识符;CUSIP;伊辛;世多乐。

这是一个更大的MERGE语句的一部分,现在我认为交叉连接正在发生在INSERT语句上:

MERGE indexdecomp.ConstituentWeighting targ
USING (#IndexConstituents src
       INNER JOIN indexDecomp.Constituent c WITH (NOLOCK) on 
        ((c.Identifier = src.Identifier) OR (c.Identifier IS NULL AND src.Identifier IS NULL))
           AND ((c.CUSIP = src.CUSIP) OR (c.CUSIP IS NULL AND src.CUSIP IS NULL))
           AND ((c.ISIN = src.ISIN) OR (c.ISIN IS NULL AND src.ISIN IS NULL))
           AND ((c.SEDOL = src.SEDOL) OR (c.SEDOL IS NULL AND src.SEDOL IS NULL)))
ON (    targ.ConstituentId = c.Id 
    AND targ.AsOfDate = src.Date
    AND ((targ.Weighting = src.Weighting) OR (targ.Weighting IS NULL AND src.Weighting IS NULL))
    AND ((targ.TotalSharesHeld = src.TotalSharesHeld) OR (targ.TotalSharesHeld IS NULL AND src.TotalSharesHeld IS NULL))
    AND ((targ.SharesOutstanding = src.SharesOutstanding) OR (targ.SharesOutstanding IS NULL AND src.SharesOutstanding IS NULL))
    AND ((targ.NotionalValue = src.NotionalValue) OR (targ.NotionalValue IS NULL AND src.NotionalValue IS NULL))
    AND ((targ.MarketValue = src.MarketValue) OR (targ.MarketValue IS NULL AND src.MarketValue IS NULL))
    AND ((targ.MarketCap = src.MarketCap) OR (targ.MarketCap IS NULL AND src.MarketCap IS NULL))
    AND ((targ.LastTrade = src.LastTrade) OR (targ.LastTrade IS NULL AND src.LastTrade IS NULL))
    AND ((targ.Earnings = src.Earnings) OR (targ.Earnings IS NULL AND src.Earnings IS NULL))
    AND ((targ.PeRatio = src.PeRatio) OR (targ.PeRatio IS NULL AND src.PeRatio IS NULL))
    AND ((targ.Face = src.Face) OR (targ.Face IS NULL AND src.Face IS NULL)))
WHEN NOT MATCHED BY TARGET THEN
    INSERT (
         ConstituentID
        ,AsOfDate
        ,Weighting
        ,TotalSharesHeld
        ,SharesOutstanding
        ,NotionalValue
        ,MarketValue
        ,MarketCap
        ,LastTrade
        ,Earnings
        ,PeRatio
        ,Face
        ,ModifiedBy
        ,ModifiedDate
        ,CreatedBy
        ,CreatedDate
    )
    VALUES (
         c.Id
        ,src.Date
        ,src.Weighting
        ,src.TotalSharesHeld
        ,src.SharesOutstanding
        ,src.NotionalValue
        ,src.MarketValue
        ,src.MarketCap
        ,src.LastTrade
        ,src.Earnings
        ,src.PeRatio
        ,src.Face
        ,'user'
        ,getdate()
        ,'user'
        ,getdate()
    )
;
4

3 回答 3

2

我认为您最近尝试的唯一问题是它没有清除所有空值的组合(即所有四个 ID 都为空的误报)。您可以通过要求四个标识符中的至少一个必须为非空来修复它,如下所示:

SELECT *
FROM xTradeCapture.staging.IndexDecomposition src
INNER JOIN indexDecomp.Constituent c WITH (NOLOCK) on 
(((c.Identifier = src.Identifier) OR (c.Identifier IS NULL AND src.Identifier IS NULL))
AND ((c.CUSIP = src.CUSIP) OR (c.CUSIP IS NULL AND src.CUSIP IS NULL))
AND ((c.ISIN = src.ISIN) OR (c.ISIN IS NULL AND src.ISIN IS NULL))
AND ((c.SEDOL = src.SEDOL) OR (c.SEDOL IS NULL AND src.SEDOL IS NULL))
AND (c.Identifier IS NOT NULL OR c.CUSIP IS NOT NULL OR c.ISIN IS NOT NULL OR c.SEDOL IS NOT NULL))
于 2012-12-17T20:38:18.590 回答
1

你的第二个代码片段是正确的。优化器识别此模式并将其转换为等连接。所以你没有得到笛卡尔积。

于 2012-12-17T20:36:16.523 回答
0

ISNULL如果您知道自己不在表中的虚拟值,您也可以使用。下面的示例假定 Identifier 和 CUSIP 为整数,ISIN 和 SEDOL 为 varchar...

SELECT *
FROM xTradeCapture.staging.IndexDecomposition src
INNER JOIN indexDecomp.Constituent c WITH (NOLOCK) on 
    ISNULL(c.Identifier,-1) = ISNULL(src.Identifier,-1)
AND ISNULL(c.CUSIP,-1) = ISNULL(src.CUSIP,-1)
AND ISNULL(c.ISIN,'ABC') = ISNULL(src.ISIN,'ABC')
AND ISNULL(c.SEDOL,'ABC') = ISNULL(src.SEDOL,'ABC')
于 2012-12-17T20:51:08.323 回答