根据价值消除结果

时间:2014-05-07 17:08:30

标签: sql

SALESID         PURCHID         ACKNUMBER   CONFIRMEDDLV
SO-0446950      PO-057272       109754      2014-04-21 00:00:00.000
SO-0446950      PO-057272       109754      2014-04-21 00:00:00.000
SO-0446950      PO-057273       109754      2014-01-10 00:00:00.000
SO-0446950      PO-057272                   1900-01-01 00:00:00.000
SO-0449613      PO-061115                   1900-01-01 00:00:00.000
SO-0449613      PO-061115                   1900-01-01 00:00:00.000
SO-0461621      PO-058821       5375543     2014-01-30 00:00:00.000
SO-0461621      PO-058821       5375543     2014-01-30 00:00:00.000

如果sales id的其中一行没有acknumber值,我想删除一个salesid。上面的正确结果仅为SO-0461621。

SELECT sal.SALESID , 
       lne.PURCHID , 
       lne.ACKNUMBER , 
       lne.CONFIRMEDDLV
  FROM
       SALESTABLE Sal LEFT JOIN SALESLINE SalLne ON Sal.SALESID = SalLne.SALESID
                      LEFT JOIN PURCHLINE Lne ON Lne.INVENTTRANSID = SalLne.INVENTREFTRANSID
  WHERE SalLne.INVENTREFID LIKE 'PO%'
    AND sal.SALESSTATUS IN( 0 , 1 )
    AND sal.SALESPOOLID <> 'FURN-CONF'
    AND sal.DLVMODE LIKE 'FURN%';

2 个答案:

答案 0 :(得分:0)

SELECT sal.SALESID , 
       lne.PURCHID , 
       lne.ACKNUMBER , 
       lne.CONFIRMEDDLV
  FROM
       SALESTABLE Sal LEFT JOIN SALESLINE SalLne ON Sal.SALESID = SalLne.SALESID
                      LEFT JOIN PURCHLINE Lne ON Lne.INVENTTRANSID = SalLne.INVENTREFTRANSID
  WHERE SalLne.INVENTREFID LIKE 'PO%'
    AND sal.SALESSTATUS IN( 0 , 1 )
    AND sal.SALESPOOLID <> 'FURN-CONF'
    AND sal.DLVMODE LIKE 'FURN%'
    and not sal.SALESID in (select SALESID from SALESTABLE where ACKNUMBER   is null);

答案 1 :(得分:0)

您不需要进行子查询,只需在WHERE子句中添加另一个条件

SELECT sal.SALESID , 
   lne.PURCHID , 
   lne.ACKNUMBER , 
   lne.CONFIRMEDDLV
FROM
   SALESTABLE Sal LEFT JOIN SALESLINE SalLne ON Sal.SALESID = SalLne.SALESID
                  LEFT JOIN PURCHLINE Lne ON Lne.INVENTTRANSID = SalLne.INVENTREFTRANSID
WHERE SalLne.INVENTREFID LIKE 'PO%'
  AND sal.SALESSTATUS IN( 0 , 1 )
  AND sal.SALESPOOLID <> 'FURN-CONF'
  AND sal.DLVMODE LIKE 'FURN%';
  AND Lne.ACKNUMBER IS NOT NULL

另外,您在帖子中说,应该返回的唯一数据是&#34; SO-0461621&#34;然而它看起来像&#34; SO-0446950&#34;在ACKNUMBER中也有一个有效值......如果这两个SALESID都被退回了吗?希望这有帮助!