两个CTE-如何在两者之间使用WHERE NOT EXISTS语句?

时间:2019-05-28 18:20:30

标签: tsql

只是想知道我在做什么错。通常,我会将它们放入临时表(使用临时表时会起作用),但是这些表具有数百万条记录,我只想与CTE一起尝试,因为我将与一些同事共享它。下面是我查询的非常简化的版本,但基本知识在这里:

WITH Apples (IDR, SpendR) AS
    (
        SELECT
            ID AS IDR
            , SUM(Cost) AS SpendR
        FROM RightFruit
        GROUP BY ID
    )
,
Oranges (IDL, SpendL) AS
    (
        SELECT
            ID AS IDL
            , SUM(Cost) AS SpendL
        FROM LeftFruit
        GROUP BY ID
    )
SELECT IDR 
FROM Apples
WHERE NOT EXISTS (SELECT 1 FROM Oranges WHERE Apples.IDR = Oranges.IDL)

运行此查询时,基本上我只是返回Apple中的内容,就好像它是SELECT * FROM。为什么WHERE NOT EXISTS不起作用?

谢谢

0 个答案:

没有答案