包括基于另一个条件动态的条件

时间:2013-07-29 15:26:55

标签: sql oracle

我的查询如下

select --------
from table a
left outer join ....c
where 
(a.column='123') and (c.column='456')

我想

  

包括“(c.column ='456')”仅当(a.column ='123')不为空时

如何在单个查询中执行此操作?或者我需要写两个单独的查询?

2 个答案:

答案 0 :(得分:0)

应该非常简单:

select --------
from table
left outer join....
where (Condition A IS NULL) OR (condition A AND condition B)

更新:根据您的条件:

where (a.column is null) or (a.column='123' and c.column='456')

如果a.column为空,或者a.columnc.column具有有效值,则会包含一行。

答案 1 :(得分:0)

据我了解你的要求,这是你想要的SQL

select distinct cm.credit_amt,e.credit_lifetime,e.credit_desc,e.icom_code,e.entry_hint,
                e.credit_id,e.credit_type_id,e.recontract_period,a.class_desc,a.offer_id,
                a.offer_class_id 
from rti_retention.package a 
left outer join rti_retention.offer_class_credit b on (a.offer_id=b.offer_id 
                                                       and a.offer_class_id=b.offer_class_id 
                                                       and a.customer_type_id=b.customer_type_id) 
left outer join rti_retention.credit_pre_bundle c on (b.credit_id=c.credit_id) 
left outer join rti_retention.credit e on (c.credit_id=e.credit_id) 
left outer join rti_retention.credit_mix_amount cm on (cm.credit_id=c.credit_id and cm.prod_mix_id=a.prod_mix_id)
where a.offer_class_id not in (1,2,16)
and a.channel_id=5 and a.customer_type_id=1 
and a.offer_id='6055' 
and c.prod_mix_id = case when (select count(*) 
                               from rti_retention.credit_pre_bundle c1 
                               where c1.prod_mix_id='1000' ) > 1 then '1000' else c.prod_mix_id end
and e.icom_code is not null

有一段时间会出现一些sql语法错误。由于我没有完整的数据库,我写了sql。不能测试它。