没有得到任何数据是最后的 select 好吗?

Not getting any data is the last select ok?

这是一个 db2 查询,但类似于 SQL 服务器。我期待这里的数据 包含没有运费信用行的所有行 (FRTCRDM)

这是订单数据。 All orders sb multi rows based on each item sold gets a row:

Order#    Item        qty    Price etc
123        Widget1     1      10.00
123        Widget2     2      20.00
123        FRTWDG      1      5.00
123        FRTCRDM     1      2.00

所以在这里,我们有 2.00 的运费折扣。我想要的是,所有没有 FRTCRDM,没有运费折扣的记录。所以我们不想要上面的例子。如果最后一行丢失,我们会想要它。

CREATE VIEW ralib.shpfrtn AS
    SELECT T01.IDORD#, T01.IDDOCD, T01.IDPRT#, 
           T01.IDSFX#, T01.IDSHP#, T01.IDNTU$, T01.IDENT# ,
           (T01.IDNTU$ * T01.IDSHP# ) AS LINTOT, 
           T02.IARCC3, T02.IAPRLC , T01.IDORDT, T01.IDHCD3 
    FROM ASTDTA.OEINDLID T01
      INNER JOIN ASTDTA.ICPRTMIA T02 ON T01.IDPRT# = T02.IAPRT# 
    WHERE t01.iddocd > 20131231
      AND t01.IDHCD3 = 'MDL'    
      AND t01.idord# NOT IN (SELECT t01.idord# FROM astdta.oeindlid b
                             WHERE idprt# = 'FRTCRDM')

where 的最后一个子句中,您需要从子查询中删除 table 前缀:

NOT IN (SELECT idord# FROM astdta.oeindlid WHERE idprt# = 'FRTCRDM')

现在您指的是外部 table t01 的 ID,而 not in 将无法正常工作。

可能是SELECT t01.idord#,它导致了相关子查询(实际上是idord# not in (idord#)总是错误的),将其更改为SELECT idord#:

SELECT T01.IDORD#, T01.IDDOCD, T01.IDPRT#, 
       T01.IDSFX#, T01.IDSHP#, T01.IDNTU$, T01.IDENT# ,
       (T01.IDNTU$ * T01.IDSHP# ) AS LINTOT, 
       T02.IARCC3, T02.IAPRLC , T01.IDORDT, T01.IDHCD3 
FROM ASTDTA.OEINDLID T01
  INNER JOIN ASTDTA.ICPRTMIA T02 ON T01.IDPRT# = T02.IAPRT# 
WHERE t01.iddocd > 20131231
  AND t01.IDHCD3 = 'MDL'    
  AND t01.idord# NOT IN (SELECT idord# FROM astdta.oeindlid b
                         WHERE idprt# = 'FRTCRDM')

或者使用 NOT EXISTS 将其更改为正确的相关子查询:

SELECT T01.IDORD#, T01.IDDOCD, T01.IDPRT#, 
       T01.IDSFX#, T01.IDSHP#, T01.IDNTU$, T01.IDENT# ,
       (T01.IDNTU$ * T01.IDSHP# ) AS LINTOT, 
       T02.IARCC3, T02.IAPRLC , T01.IDORDT, T01.IDHCD3 
FROM ASTDTA.OEINDLID T01
  INNER JOIN ASTDTA.ICPRTMIA T02 ON T01.IDPRT# = T02.IAPRT# 
WHERE t01.iddocd > 20131231
  AND t01.IDHCD3 = 'MDL'    
  AND NOT EXISTS
    (SELECT * FROM astdta.oeindlid b
     WHERE idprt# = 'FRTCRDM'
       AND t01.idord# = b.idord#)