SQL Server 2005 - NOT EXISTS中没有EXESTS和复杂的脚本

时间:2014-12-28 05:02:29

标签: sql-server sql-server-2005 not-exists cross-apply

脚本目标:从列表中获取数字列表,其中数字未显示在另一个列表中。

复杂性:另一个数字列表只能通过一个复杂的脚本获得 - 由于某种原因,当我知道应该存在时,我没有得到任何结果;因为第一个列表将包含所有数字,第二个数字列表将只包含一些数字;所以我应该得到一些结果。

我写的脚本(审查)

SELECT A.Number
FROM   sometable AS A
       INNER JOIN othertable AS B
               ON A.Data = B.Data
       INNER JOIN othertable2 AS C
               ON B.Data = C.Data
       INNER JOIN othertable3 AS D
               ON C.Data = D.Data
WHERE  D.Data = 'int'
       AND NOT EXISTS (SELECT DISTINCT A.Number
                       FROM   sometable AS C
                              anothertable AS B
                                      ON C.Data = B.Data
                              INNER JOIN anothertable AS E
                                      ON B.Data = E.Data
                              INNER JOIN anothertable AS A
                                      ON E.Data = A.Data
                              CROSS apply (SELECT DG.Data
                                           FROM   atable AS DG
                                           WHERE  B.Data = DG.Data) D
                       WHERE  D.Data IN ( 'int', 'int', 'int', 'int' )) 

如果我运行part1(在不存在之前)它可以正常运行

如果我运行part2(不存在的数据)它也可以正常工作 - 具有不同的结果(包含来自part1的数字)

但他们在一起却没有。所以如果不存在我不需要使用的话,我需要知道如何做到这一点?

2 个答案:

答案 0 :(得分:0)

你说子查询独立运行会产生结果。因此,当在NOT EXISTS子句中运行时,它将始终产生结果,因此该子句将始终为false。

我的猜测是你的意思更像是WHERE A.Number NOT IN ( ... )

答案 1 :(得分:0)

您的查询正常运行。由于您的子查询中存在某些行,因此外部查询不会产生任何结果,但这不是完成目标的正确方法

您的目标

  

从列表中获取数字列表,其中的数字不会显示在另一个列表中。

可以通过两种方式完成。

使用Not Exists

SELECT A.Number
FROM   sometable AS A
       INNER JOIN othertable AS B
               ON A.Data = B.Data
       INNER JOIN othertable2 AS C
               ON B.Data = C.Data
       INNER JOIN othertable3 AS D
               ON C.Data = D.Data
WHERE  D.Data = 'int'
       AND NOT EXISTS (SELECT 1
                       FROM   sometable AS CC
                              INNER JOIN anothertable AS BB
                                      ON Cc.Data = BB.Data
                              INNER JOIN anothertable AS EE
                                      ON BB.Data = EE.Data
                              INNER JOIN anothertable AS AA
                                      ON EE.Data = AA.Data
                              CROSS apply (SELECT DG.Data
                                           FROM   atable AS DG
                                           WHERE  BB.Data = DG.Data) DD
                       WHERE  DD.Data IN ( 'int', 'int', 'int', 'int' )
                              AND aa.number = a.number)

或使用Not IN

SELECT A.Number
FROM   sometable AS A
       INNER JOIN othertable AS B
               ON A.Data = B.Data
       INNER JOIN othertable2 AS C
               ON B.Data = C.Data
       INNER JOIN othertable3 AS D
               ON C.Data = D.Data
WHERE  D.Data = 'int'
       AND A.Number NOT IN (SELECT AA.number
                            FROM   sometable AS CC
                                   INNER JOIN anothertable AS BB
                                           ON Cc.Data = BB.Data
                                   INNER JOIN anothertable AS EE
                                           ON BB.Data = EE.Data
                                   INNER JOIN anothertable AS AA
                                           ON EE.Data = AA.Data
                                   CROSS apply (SELECT DG.Data
                                                FROM   atable AS DG
                                                WHERE  BB.Data = DG.Data) DD
                            WHERE  DD.Data IN ( 'int', 'int', 'int', 'int' )
                                   AND aa.number = a.number)