Oracle:检查元组中的 NOT NULL

Oracle : Checking for NOT NULL in Tuples

Objective : 从订单 table 到 select 记录,其中 (delivery_date, 类型) 不在 (NULL, 'A').

select * from Orders; 

 Table : Orders

 No     Type  Delivery_Date
  1      A      null
  2      B      20150120
  3      A      20150115
  4      A      20150115
  5      A      20150111
  6      A      20150112
  7      B      null
  8      B      null

预期结果:

  No    Type  Delivery_Date

  2      B      20150120
  3      A      20150115
  4      A      20150115
  5      A      20150111
  6      A      20150112
  7      B      null
  8      B      null

在 where 子句中尝试了以下约束,但没有成功。

1. WHERE (DELIVERY_DATE, TYPE) IS NOT IN (NULL, 'A')
2. WHERE (NVL(DELIVERY_DATE, 0), TYPE) IS NOT IN (0, 'A')

为了让它工作,添加了一个名称为 required_row 的列,如果此条件为(deliver_date 为空且类型 = 'A')且 select只读取 required_row 为 Y 的记录。

with orders
as 
  (select 1 as no, 'A' as type, null as delivery_date from dual union 
   select 2 as no, 'B' as type, 20150120 as delivery_date from dual union  
   select 3 as no, 'A' as type, 20150115 as delivery_date from dual union 
   select 4 as no, 'A' as type, 20150115 as delivery_date from dual union 
   select 5 as no, 'A' as type, 20150111 as delivery_date from dual union
   select 6 as no, 'A' as type, 20150112 as delivery_date from dual union
   select 7 as no, 'B' as type, null as delivery_date from dual union
   select 8 as no, 'B' as type, null as delivery_date from dual
  )
   select * from ( select orders.*, 
   case when orders.delivery_date is null and type = 'A' 
        then 'N' else 'Y' 
        end as required_row from orders) where required_row='Y';

对于以任何其他方法实现相同目标并保持性能的任何意见/想法,我们将不胜感激。

试试这个

select orders.* from orders where Delivery_Date is not null or type !='A'
 /*Assuming type as a char field and this query will output all records 
             excluding deliverydate_null with type ='A' */

修改了上述查询以包含 fiddle 中共享的 sql 片段。

更新:

这是示例 SQLFIDDLE

可以使用不存在的子查询来解决此问题:

SELECT * FROM order t 
WHERE not exists (
  SELECT 1 
  FROM order 
  WHERE 
    type = 'A' 
    and delivery_date is null 
    and id = t.id
)