0

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

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

我写的剧本(审查)

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' )) 

如果我运行第 1 部分(在不存在之前)它工作正常

如果我运行第 2 部分(不存在的数据),它也可以正常工作 - 结果不同且更少(包含第 1 部分中的数字)

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

4

2 回答 2

0

您说子查询独立运行会产生结果。因此,在NOT EXISTS子句中运行时,它总是会产生结果,因此子句将始终为假。

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

于 2014-12-28T05:12:39.290 回答
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) 
于 2014-12-28T05:37:03.230 回答