两张表 A, B 字段一样如下 f1  f2  f3 f4 time
A 表数据 f1  f2  f3 f4 time
 1    2        12:00
 3        4    13:00B 表数据 f1  f2  f3 f4 time
         12  13  12:00
     15      16得到查询结果如下:
 f1  f2  f3 f4 time
 1    2  12 13 12:00
 3   15   4 16 13:00相当于合并两个表,空数据的地方取另一个表的数据来填充。我用的是orcal数据库。真心求一sql语句,非常感谢。分不够我可以再开贴加

解决方案 »

  1.   

    麻烦你A表B表的数据排下格式。。OK?
      

  2.   

    排好的,怎么能乱成这样子,晕
    重启排一下看看A 表数据 f1 f2 f3 f4 time
     1  2        12:00
     3     4     13:00B 表数据 f1 f2 f3 f4 time
           12 13 12:00
        15    16 13:00
    得到查询结果如下:
     f1 f2 f3 f4 time
     1  2  12 13 12:00
     3 15  4  16 13:00
      

  3.   


    排好的,怎么能乱成这样子,晕
    重启排一下看看A 表数据 f1 f2 f3 f4 time
     1  2        12:00
     3     4     13:00B 表数据 f1 f2 f3 f4 time
           12 13 12:00
        15    16 13:00
    得到查询结果如下:
     f1 f2 f3 f4 time
     1  2  12 13 12:00
     3 15  4  16 13:00
      

  4.   

       问题是你 找到 B 的哪条记录 去填  如果 B 中还有数据为 
      f1 f2 f3 f4 time 
            20 21 12:00
     
     那跟12 13 那条 到底选择哪条?
      

  5.   

    根据time来关联?
    select a.f1||b.f1,a.f2||b.f2,a.f3||b.f3,a.f4||b.f4,a.time
    from A,B
    where a.time=b.time
      

  6.   

    如果一个时间都是一个空格对呀一个数字的话  这样应该可以了
    select a.f1||b.f1 f1,a.f2||b.f2 f2 ,a.f3||b.f3 f3 ,a.f4||b.f4 f4,a.time
    from A,B
    where a.time=b.time
      

  7.   

     
    select nvl(a.f1, b.f1) f1 ,nvl(a.f2,b.f2) f2 ,nvl(a.f3,b.f3) f3 ,nvl(a.f4,b.f4) f4 from testA a , testB b where a.time=b.time ;
       如果是按照时间关联  上面的语句即可满足要求
      

  8.   


    create table taba
    (f1 int, f2 int, f3 int, f4 int, times varchar(7))insert into taba
    select 1,2,null,null,'12:00' union all
    select 3,null,4,null,'13:00'create table tabb
    (f1 int, f2 int, f3 int, f4 int, times varchar(7))insert into tabb
    select null,null,12,13,'12:00' union all
    select null,15,null,16,'13:00'
    select 
    coalesce(a.f1,b.f1) f1,
    coalesce(a.f2,b.f2) f2,
    coalesce(a.f3,b.f3) f3,
    coalesce(a.f4,b.f4) f4,
    a.times
    from taba a
    inner join tabb b
    on a.times=b.timesf1          f2          f3          f4          times
    ----------- ----------- ----------- ----------- -------
    1           2           12          13          12:00
    3           15          4           16          13:00(2 row(s) affected)
      

  9.   


    select sum(nvl(f1,0)) f1,sum(nvl(f2,0)) f2,sum(nvl(f3,0)) f3,sum(nvl(f4,0)) f4,time
    from
    (select * from A
    union all
    select * from B)
    group by time
      

  10.   

        如果不想写字段  如果不用存储过程   单纯的用sql 貌似很难
      

  11.   

    SELECT 
    NVL(A.F1, B.F1) F1 ,NVL(A.F2,B.F2) F2 ,NVL(A.F3,B.F3) F3 ,NVL(A.F4,B.F4) F4 ,A.TIME
    FROM TESTA A , TESTB B 
    WHERE A.TIME=B.TIME 
    ;
      

  12.   

    drop table atd.resolve_20120216;
    create table atd.resolve_20120216_1(f1 int,f2 int,f3 int,f4 int,cur_time time);
    insert into atd.resolve_20120216_1 values(1,2,null,null,'12:00'),(3,null,4,null,'13:00');
    select * from atd.resolve_20120216_1;
    create table atd.resolve_20120216_2 like atd.resolve_20120216_1;
    insert into atd.resolve_20120216_2 values(null,null,12,13,'12:00'),(null,15,null,16,'13:00');
    select * from atd.resolve_20120216_2;select nvl(a.f1,b.f1),nvl(a.f2,b.f2),nvl(a.f3,b.f3),nvl(a.f4,b.f4),a.cur_time
    from atd.resolve_20120216_1 a,atd.resolve_20120216_2 b
    where a.cur_time=b.cur_time
      

  13.   

    个人推荐用coalesceCOALESCE函数说明:
    COALESCE (expression_1, expression_2, ...,expression_n)
    列表中第一个非空的表达式是函数的返回值,如果所有的表达式都是空值,最终将返回一个空值。
    with tab_a as
    (
    select '1' f1, '2'  f2, NULL f3, NULL f4, '12:00' times from dual
    union all
    select '3' f1, NULL f2, '4'  f3, NULL f4, '13:00' times from dual
    ), 
    tab_b as 
    (
    select NULL f1, NULL f2, '12' f3, '13' f4, '12:00' times from dual
    union all
    select NULL f1, '15' f2, NULL f3, '16' f4, '13:00' times from dual
    )select 
    coalesce(a.f1,b.f1) f1,
    coalesce(a.f2,b.f2) f2,
    coalesce(a.f3,b.f3) f3,
    coalesce(a.f4,b.f4) f4,
    a.times
    from tab_a a
    inner join tab_b b
    on a.times=b.times执行结果:
    F1 F2 F3 F4 TIMES 
    -- -- -- -- ----- 
    1  2  12 13 12:00 
    3  15 4  16 13:00 
      

  14.   


    --oracle,我试了下用4个语句实现
    create table a
    (
     f1 varchar(20),
     f2 varchar(20),
     f3 varchar(20),
     f4 varchar(20),
     t varchar(20)
    )
    create table b
    (
     f1 varchar(20),
     f2 varchar(20),
     f3 varchar(20),
     f4 varchar(20),
     t varchar(20)
    )
    --
    insert into a values('1','2','','', '11:00')
    insert into b values('','','21','22', '11:00')
    insert into a values('5','6','','', '12:00')
    insert into b values('','','15','16', '12:00')
    --
    --现在将数据都合并到a表
    update a set f1=  (select f1 from b where a.t=b.t  ) where f1 is null 
    update a set f2=  (select f2 from b where a.t=b.t  ) where f2 is null 
    update a set f3=  (select f3 from b where a.t=b.t  ) where f3 is null 
    update a set f4=  (select f4 from b where a.t=b.t  ) where f4 is null 
     
    已经测试过了,可以实现
      

  15.   

    使用merge可以一个sql语句完成 merge into a aa      
    using b bb           
    on (aa.t=bb.t)       
    when matched then       
    update set
    aa.f1=(case when bb.f1 is not null then bb.f1 else aa.f1  end ),
    aa.f2=(case when bb.f2 is not null then bb.f2 else aa.f2 end ) ,
    aa.f3=(case when bb.f3 is not null then bb.f3 else aa.f3 end )  ,
    aa.f4=(case when bb.f4 is not null then bb.f4 else aa.f4 end )        
    when not matched then    
    insert values( bb.f1, bb.f2, bb.f3,bb.f4,bb.t);
      

  16.   

    select nvl(a.f1, b.f1) f1 ,
              nvl(a.f2,b.f2) f2 ,
              nvl(a.f3,b.f3) f3 ,
              nvl(a.f4,b.f4) f4 ,
              a.time
    from A a , B b where a.time=b.time ;