用于比较WHERE子句中的操作的SQL异常

时间:2015-08-24 07:18:55

标签: sql oracle conditional-statements where

我将这两个表连接在一起,并且我发表了一个声明,期间必须是> 2013年10月1日。但我希望这句话对代码=' A722'和host_country ='波兰'。我尝试在where子句中使用case语句,但它似乎没有工作,我似乎无法弄清楚原因。对此有何选择?

with table1 (person_id, period) as (
   select 12, date '2013-08-01' from dual union all
   select 13, date '2015-03-16' from dual union all
   select 14, date '2015-04-22' from dual union all
   select 15, date '2015-06-13' from dual
 ),
 table2 (person_id, code, host_country) as (
   select 12, 'A8722', 'poland' from dual union all
   select 13, 'B8422', 'china' from dual union all
   select 14, 'Z8725', 'sweden' from dual union all
   select 15, 'G8726' ,'Germany' from dual
 ) 

 select 
 t1.person_id, 
 t2.code, 
 t2.host_country,
 t1.period 
 from table1 t1, table2 t2 
 where t1.person_id=t2.person_id
 AND t1.period> CASE WHEN t2.code NOT IN ('A8722') AND t2.host_country NOT IN('poland') THEN to_date('2013.10.01','YYYY.MM.DD') END

链接到sqlfiddle

0 个答案:

没有答案