Sql-Filter the Select

时间:2013-02-01 05:47:31

标签: sql oracle oracle10g

SELECT DISTINCT x1.NUM_KANBAN_ID ID, 
                TO_CHAR(x1.DT_CREATE,'YYYY-MM-DD') CREATED,
                x5.TXT_PLANT_CD CD,
                TO_CHAR(x4.DT_FIRST_VIEWED,'YYYY-MM-DD') VIEWED,
                TO_CHAR(x4.DT_DUE,'YYYY-MM-DD') DUE, x4.
                TXT_SHUTTLE_NUMBER SHUTTLE, 
                (select count(*) 
                 from WEDI_ASN_TO_DIMENSION t8 
                 where t8.TXT_MATCH_CD='1' 
                 AND t8.NUM_DIMENSION_ID=x2.NUM_DIMENSION_ID AND t8.NUM_MATCH_ID = x1.NUM_KANBAN_ID) ASN, 
                 x1.TXT_JIT_CALL_NUM,
                 x2.NUM_VENDOR_ID VENDOR_ID 
 FROM WEDI_KANBAN x1, WEDI_DIMENSION x2, WEDI_USER_VENDORS x3, WEDI_KANBAN_TO_DIMENSION x4, WEDI_PLANT x5
 WHERE x5.NUM_PLANT_ID = x2.NUM_PLANT_ID AND 
       x1.TXT_STATUS_CD = 'C' AND
       x2.NUM_VENDOR_ID = x3.NUM_VENDOR_ID AND 
       x4.NUM_DIMENSION_ID = x2.NUM_DIMENSION_ID AND 
       x1.NUM_KANBAN_ID = x4.NUM_KANBAN_ID AND 
       x1.DT_CREATE < SYSDATE - 15 AND
       x5.TXT_PLANT_CD LIKE '%'  AND
       x2.TXT_MATERIAL_NUM LIKE '%' AND 
       ((x4.TXT_SHUTTLE_NUMBER IS NULL) OR (x4.TXT_SHUTTLE_NUMBER LIKE '%')) AND 
       x4.DT_DUE < SYSDATE - 10 
 ORDER BY VENDOR_ID

在上面的查询中,我应该显示其ASN仅为1或2的id。如果ASN有这样的0,1,2我不应该显示这些ID。通常,如果id的ASN为0,则不应显示。 请建议

2 个答案:

答案 0 :(得分:1)

最简单的解决方案是:

select * 
from (your query)
where 0 not in (select ASN from (your query))

为了不再写两次查询:

with (your query) as A
select * 
from a 
where 0 not in (select asn from a)

with (your query) as A
select * 
from a 
where not exists (select * from a where asn = 0)

但是,当存在一个等于零的ASN时,存在重写查询以不返回任何行的可能性,但查询很难理解,并且几乎可以确定某些内容是错误的,因为您需要一个DISTINCT子句。那是不好的迹象。

答案 1 :(得分:0)

在Where语句中添加(ASN = 1) Or (ASN = 2)之类的内容