2016-01-28 48 views
-1

在下面,我試圖選擇第一部分中選擇的數據,不應該存在於發票表中。我們希望不開發票。選擇行,如果訂單#不在第三個表中

# part 1 
CREATE VIEW Stock.CLSPAYB2 AS 
SELECT T02.OHCOM#, T02.oHPTTC, T02.oHSLR#, T01.OTTRT, T01.OTORD#, T02.ohord#, T01.OTTRND, T02.oHORDT, T02.oHordd, T02.ohttn$, t02.OHHLDC, T01.OTUSRN, T01.OTTRNC 
FROM Sales.OETRANOT T01 INNER JOIN Stock.OEORHDOH T02  
ON 
    T01.OTCOM# = T02.oHCOM# 
AND 
    T01.OTORD# = T02.oHORD# 
WHERE   
    T01.OTTRNC IN ('BCS') 
AND 
    t02.ohordd >= 20150101 

# Part 2. Here is the issue: 

AND t02.ohord#  
NOT IN 
    (
    SELECT * FROM 
    Stock.OEINHDIH t03 
    WHERE t02.ohord# = t03.IHORD# 
    );           

回答

1

你想要麼NOT INNOT EXISTS,但你混合兩個語法。只需使用:

NOT EXISTS (SELECT 1 
      FROM Stock.OEINHDIH t03 
      WHERE t02.ohord# = t03.IHORD# 
      ) 

或者,如果你喜歡:

t02.ohord# NOT IN (SELECT t03.IHORD# FROM Stock.OEINHDIH t03) 
相關問題