我需要帮助搞清楚为什么我的sql where子句在我添加final&#34时不起作用;而不是"声明

时间:2017-03-13 17:35:08

标签: sql-server

SELECT 
    rec.CONSTITUENT_ID as 'RE Const ID', 
    CASE 
       WHEN soft.giftID IS NULL AND con.first_name IS NULL 
          THEN con.Key_Name
       WHEN soft.giftID IS NULL AND con.first_name IS NOT NULL 
          THEN con.first_name + ' ' + con.KEY_NAME
       WHEN soft.giftID IS NOT NULL AND con3.first_name IS NULL 
          THEN con3.Key_Name
       WHEN soft.giftID IS NOT NULL AND con3.first_name IS NOT NULL 
          THEN con3.first_name + ' ' + con3.KEY_NAME
    END as 'Constituent Name',
    gift.usergiftid AS 'RE Gift ID', 
    CONVERT(varchar(12),gift.dte, 10) AS 'Gift Date', 
    (CASE 
        WHEN soft.amount IS NULL 
           THEN gift.amount 
        WHEN soft.amount IS NOT NULL 
           THEN soft.amount 
     END) AS 'Total Gift Amount', 
     CASE 
        WHEN soft.ConstitID IS NOT NULL 
           THEN 'Yes' 
           ELSE 'No' 
     END AS 'Soft Credited?'
FROM 
    dbo.gift gift
LEFT OUTER JOIN 
    dbo.GiftSoftCredit soft ON soft.giftID = gift.id and soft.giftID IS NOT NULL
LEFT OUTER JOIN 
    dbo.records rec ON (CASE WHEN soft.giftID IS NOT NULL THEN soft.ConstitID WHEN soft.giftID IS NULL THEN gift.constit_ID END) = rec.ID
LEFT OUTER JOIN 
    dbo.constituent con ON con.records_ID = rec.ID 
                        AND con.spousename = '0' 
                        AND con.sequence = '0' 
                        AND soft.GiftID IS NULL 
LEFT OUTER JOIN 
    dbo.constituent con3 ON con3.RECORDS_ID = rec.id 
                         AND soft.GiftID IS NOT NULL 
                         AND con3.spousename = 0 
                         AND con3.sequence = 0
LEFT OUTER JOIN 
    dbo.GiftSolicitor sol ON sol.parentID = gift.id AND sol.sequence = 1
LEFT OUTER JOIN 
    dbo.constituent con2 ON con2.RECORDS_ID = sol.SolicitorId 
                         AND con2.sequence = 0 
                         AND con2.spousename = 0
WHERE 
    gift.dte BETWEEN '2016-10-01' AND '2016-10-08'
    AND gift.amount > 0.0000 
    AND con2.records_ID IS NULL
    AND gift.type IN ('1','2','3','31')

导致此无效的第二部分

    AND rec.constituent_ID NOT IN 
        (SELECT distinct rec.constituent_ID 
         FROM dbo.gift gift
         LEFT OUTER JOIN dbo.GiftSoftCredit soft ON soft.giftID = gift.id
         LEFT OUTER JOIN dbo.records rec ON (CASE WHEN soft.giftID IS NOT NULL THEN soft.ConstitID WHEN soft.giftID IS NULL THEN gift.constit_ID END) = rec.ID
         WHERE gift.dte BETWEEN '1991-01-01' AND '2016-09-30')

目标是我将从查询的顶部获取不在底部子查询中的记录。

当我自己运行sql的顶部时,我得到478条记录。当我在代码的底部运行select时,我得到100970条记录。当我将excel中的这些列表与vlookup进行比较时,有173个constituent_id记录不在该子查询中。但是当我一起运行查询时,我没有记录。

我不知道我在这里做错了什么,我仍然在很大程度上学习了一些,但是我没有人去寻求帮助因为我都是自学成才。我希望我能在这里得到一些帮助。

我在SQL Server 2008 R2(SP2)上。

1 个答案:

答案 0 :(得分:0)

从此链接:NOT IN clause and NULL values

select 'true' where 3 not in (1, 2, null)什么都不返回,你的SQL可能会为最后一节中的rec.constituent_ID列返回NULL。

如果您将LEFT OUTER JOINS更改为INNER JOINS,逻辑是否仍然有效? INNER JOINS将删除NULL。