如果订单#不在第3个表中,请选择行

时间:2016-01-28 00:01:14

标签: sql db2

在下面,我试图选择,在第一部分中选择的数据不应存在于发票表中。我们想要非发票。

# 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 个答案:

答案 0 :(得分: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)