为什么相同的 EXISTS returns 不同的结果

Why same EXISTS returns different result

每个 PolicyNumber 可以有多个 ClassCode。所以目标是消除所有相关列的整个 PolicyNumber 如果至少有一个 ClassCode 选择被淘汰,则包括所有 ClassCode's。 我有 cte1PolicyNumberWrittenPremium(WP),我有 table tblClassCodesPlazaCommercialPolicyNumberClassCode。 在我选择的 WHERE 子句中:

WHERE       EXISTS (
                    SELECT  DISTINCT PolicyNumber 
                    FROM    tblClassCodesPlazaCommercial 
                    WHERE   PolicyNumber NOT IN (SELECT  PolicyNumber FROM tblClassCodesPlazaCommercial WHERE  ClassCode =5151)
                    )

然后如果我 select * from cte3 WHERE ClassCode =5151 我仍然有 ClassCode 5151 的政策

下面的整个 select 语句:

 select     
                cte1.PolicyNumber,
                cte1.TransactionEffectiveDate,
                cc.ClassCode,
                CASE
                    WHEN ROW_NUMBER() OVER (PARTITION BY cte1.QuoteID, cte1.PolicyNumber, cc.TransactionEffectiveDate ORDER BY (SELECT 0))=1 THEN cte1.WP 
                    ELSE 0
                END  as WP--,
    from        cte1 inner join tblClassCodesPlazaCommercial cc on cte1.PolicyNumber=cc.PolicyNumber AND cte1.QuoteID=cc.QuoteID AND cte1.TransactionEffectiveDate=cc.TransactionEffectiveDate 
    where       EXISTS (SELECT DISTINCT PolicyNumber FROM tblClassCodesPlazaCommercial WHERE PolicyNumber NOT IN (SELECT  PolicyNumber FROM tblClassCodesPlazaCommercial WHERE  ClassCode =5151))

但是,如果我用没有 ClassCode 5151 的 PolicyNumber 声明 @PoliciesThatDontHaveClassCodes Table 并在我的 EXISTS 命令中使用它 - 那么它就可以工作。

DECLARE @PoliciesThatDontHaveClassCodes Table (PolicyNumber varchar(100)) 
INSERT INTO @PoliciesThatDontHaveClassCodes SELECT DISTINCT PolicyNumber    FROM tblClassCodesPlazaCommercial 
                                                                            WHERE PolicyNumber NOT IN (SELECT  PolicyNumber FROM tblClassCodesPlazaCommercial WHERE  ClassCode =5151)
select      
            cte1.PolicyNumber,
            cte1.TransactionEffectiveDate,
            cc.ClassCode,
            CASE
                WHEN ROW_NUMBER() OVER (PARTITION BY cte1.QuoteID, cte1.PolicyNumber, cc.TransactionEffectiveDate ORDER BY (SELECT 0))=1 THEN cte1.WP 
                ELSE 0
            END  as WP--,
from        cte1 inner join tblClassCodesPlazaCommercial cc on cte1.PolicyNumber=cc.PolicyNumber 
            AND cte1.QuoteID=cc.QuoteID 
            AND cte1.TransactionEffectiveDate=cc.TransactionEffectiveDate 
where       EXISTS (SELECT * FROM @PoliciesThatDontHaveClassCodes t  WHERE t.PolicyNumber=cc.PolicyNumber )

有什么区别?这两个子查询本身 returns 完全相同的结果集。为什么在第一种情况下它不起作用,但在第二种情况下(当我声明@PoliciesThatDontHaveClassCodes 时)- 它起作用了? 如何在不声明任何 table 变量的情况下获得相同的结果?

在您提供的第一个样本中

WHERE EXISTS (SELECT  DISTINCT PolicyNumber 
                FROM    tblClassCodesPlazaCommercial 
                WHERE   PolicyNumber NOT IN (SELECT  PolicyNumber 
                                               FROM tblClassCodesPlazaCommercial 
                                               WHERE  ClassCode =5151))

外部查询和子查询之间没有关联。因此,我们单独评估子查询,并且由于查询 returns 一行,因此 WHERE EXISTS … 始终为真。

在第二个示例中,您将子查询与外部查询相关联,这就是 t.PolicyNumber = cc.PolicyNumber 所做的,因为 cc 在子查询之外:

WHERE EXISTS (SELECT * FROM @PoliciesThatDontHaveClassCodes t  
               WHERE t.PolicyNumber=cc.PolicyNumber )

因此子查询的结果(可能)对于外部查询的每一行都是不同的。

因此两个 WHERE 子句的计算方式不同。

编辑

可能修改第一个谓词

WHERE NOT EXISTS (Select 1 From tblClassCodesPlazaCommercial As t
                           Where t.PolicyNumber = cc.PolicyNumber
                             And t.ClassCode = 5151)