select t1.id,(
 case when id is not null then t1.ksrq
 case when id is null  then to_date(max(t2.yksrq)||'-12-01','yyyy-mm-dd' 
 end)
from t1,t2 where t1.id=t2.id

解决方案 »

  1.   

    create view v1 as 
    select decode(nvl(t1.ksrq,'NULL'),'NULL',t2.id,t1.id) id,
           decode(nvl(t1.ksrq,'NULL'),'NULL',to_date('max(yksrq)-12-01','yyyy-mm-dd'),t1.ksrq) rq
    from t1,t2 where t1.id=t2.id
      

  2.   

    如果t1,t2有关联,这样写应该也可以:
    create or replace view v1
    (id , ksrq)
    as
    (select id,ksrq from t1
    where id is not null
    union
    select t2.id,to_date('max(yksrq)-12-01','yyyy-mm-dd')
    from t2,t1
    where t1.id=t2.id 
    and t1.id is null)
      

  3.   

    看这个我经过测试的: 
    假如你要的是内连接:
    select t1.id,(
      case when t1.ksrq is not null then t1.ksrq
     when t1.ksrq is null  then to_date(t3.ksrq||'-12-01','yyyy-mm-dd')
      end) ksrq
     from t1,(select id,max(yksrq) ksrq from t2 group by id) t3 where t1.id=t3.id
    /
      

  4.   

    楼上的,想问一下 case when 能不能在8i下用啊,谢谢
      

  5.   

    create or replace view v1 as
      select *
        from t1 where t1.ksrq is not null
      union
      select t2.id,to_date(max(t2.yksrq)||'-12-01','yyyy-mm-dd') as ksrq
        from t1,t2 where t1.id=t2.id group by t2.id
      

  6.   

    sorry,上面写错了一点,漏了一点条件
    create or replace view v1 as
      select *
        from t1 where t1.ksrq is not null
      union
      select t2.id,to_date(max(t2.yksrq)||'-12-01','yyyy-mm-dd') as ksrq
        from t1,t2 where t1.id=t2.id and t1.ksrq is null group by t2.id
      

  7.   

    楼上的,想问一下 case when 能不能在8i下用啊,谢谢
    -----------------------------------------------------
    我查了一下手册,发现8.1.7上可以使用case when