2

次の列と結合したい 2 つのテーブルがあります。

テーブル: #IndexDecomposition; 構成要素。

行: 識別子。CUSIP; ISIN; セドル。

これはすべて、より大きな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

あなたの最新の試みの唯一の問題は、すべてのnullの組み合わせを除外しないことだと思います(つまり、4つのIDすべてがnullである誤検知)。次のように、4 つの識別子のうち少なくとも 1 つが非 null でなければならないことを要求することで、これを修正できます。

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

あなたの2番目のコードスニペットは正しいです。オプティマイザはこのパターンを認識し、それを等結合に変換します。したがって、デカルト積は得られません。

于 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 に答える