数据库表如下:
t1
id      varchar2(10)      not null
ksrq    date
主键:id
t2
id      varchar2(10)      not null
yksrq   varchar2(4)       not null
主键:id,yksrq
实际情况下:
t1中的ksrq可能是null,t2中的同一个id可以有多个yksrq,yksrq的值为4位年份如'2002','2001'等
需要建立视图v1:
v1中含有:id和ksrq两个字段
首选当t1中的ksrq不是null时,v1中的id,ksrq就是t1的对应内容,
当t1中的ksrq是null时,v1中的id,ksrq为:t2中的id,to_date('max(yksrq)-12-01','yyyy-mm-dd')请各位帮帮忙!!!!谢谢,比较着急,在线等候!

解决方案 »

  1.   

    select id,ksrq from t1 where ksrq is not null
    union
    select t1.id, to_date('max(yksrq)-12-01','yyyy-mm-dd') from t1,t2 where ksrq is null and t1.id=t2.id
      

  2.   

    select t1.id,t2.yksrq from t1,t2 where t1.id=t2.id and t1.ksrq is not null
    union
    select t1.id,t2._date('max(yksrq)-12-01','yyyy-mm-dd') from t1,t2
    where t1.id=t2.id and t1.ksrq 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.   

    兄弟:我本是第一个占地的,可是死机了!!哎。creat view as
    select * from t1 where ksrq is not null 
    union
    select t2.id,to_date('max(t2.yksrq)-12-01','yyyy-mm-dd') from t1,t2 
              where t1.id is null 
                and t1.id=t2.id;