其中column.a不为null,除非column.b = value

时间:2016-02-19 23:35:33

标签: sql teradata

我有一个提取数据的查询。这是我的WHERE条款。

where p.invc_dt  BETWEEN 1150101 AND 1160131    
and o.Tracking_num is not null 

此表包含订单数据和ORDER_CD列,用于标识销售类型或是否已退款。退款关联跟踪号。我怎样才能调整我的where语句来说出像

这样的内容
and o.Tracking_num is not null **unless** `ORDER_CD` is REFUND

2 个答案:

答案 0 :(得分:1)

您可以使用布尔逻辑实现逻辑:

where p.invc_dt  BETWEEN 1150101 AND 1160131 and
      (o.Tracking_num is not null or order_cde = 'REFUND')

我不确定“除非”是否属于排他性,所以你可能意味着:

where p.invc_dt  BETWEEN 1150101 AND 1160131 and
      ((o.Tracking_num is not null and order_cde <> 'REFUND')
       (o.Tracking_num is null and order_cde = 'REFUND')
      )

答案 1 :(得分:1)

where p.invc_dt  BETWEEN 1150101 AND 1160131    
and ( o.Tracking_num is not null OR `ORDER_CD` = 'REFUND' )