100 分 求一sql语句,该怎么处理

100 分 求一sql语句两张表 A, B 字段一样如下f1f2f3 f4 timeA 表数据f1f2f3 f4 time1212:003413:00B 表数

100 分 求一sql语句
两张表 A, B 字段一样如下

 f1 f2 f3 f4 time




A 表数据

 f1 f2 f3 f4 time
 1 2 12:00
 3 4 13:00

B 表数据

 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语句,非常感谢。

分不够我可以再开贴加




[解决办法]
A表和B表的数据与字段的对应有点混乱
[解决办法]
问题是你 找到 B 的哪条记录 去填

如果 B 中还有数据为

 

XML code
  f1 f2 f3 f4 time         20 21 12:00
[解决办法]
是不是还少个主要字段啊 2个如何关联起来呢?
[解决办法]
-- 一个重要的问题没搞明白:A表和B表是怎么关联记录行滴?
[解决办法]
根据time来关联?

SQL code
select a.f1||b.f1,a.f2||b.f2,a.f3||b.f3,a.f4||b.f4,a.timefrom A,Bwhere a.time=b.time
[解决办法]
如果一个时间都是一个空格对呀一个数字的话 这样应该可以了

SQL code
select a.f1||b.f1 f1,a.f2||b.f2 f2 ,a.f3||b.f3 f3 ,a.f4||b.f4 f4,a.timefrom A,Bwhere a.time=b.time
[解决办法]
SQL code
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 ;
[解决办法]
SQL code
create table taba(f1 int, f2 int, f3 int, f4 int, times varchar(7))insert into tabaselect 1,2,null,null,'12:00' union allselect 3,null,4,null,'13:00'create table tabb(f1 int, f2 int, f3 int, f4 int, times varchar(7))insert into tabbselect null,null,12,13,'12:00' union allselect 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.timesfrom taba ainner join tabb bon a.times=b.timesf1          f2          f3          f4          times----------- ----------- ----------- ----------- -------1           2           12          13          12:003           15          4           16          13:00(2 row(s) affected)
[解决办法]
SQL code
select sum(nvl(f1,0)) f1,sum(nvl(f2,0)) f2,sum(nvl(f3,0)) f3,sum(nvl(f4,0)) f4,timefrom(select * from Aunion allselect * from B)group by time
[解决办法]
如果不想写字段 如果不用存储过程 单纯的用sql 貌似很难
[解决办法]
探讨

SQL code

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 in……

[解决办法]
[Quote=引用:]
补充一点:
SQL code
SQL code
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.timefrom testA a , testB b where a.time=b.time ;
[解决办法]


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 
;
[解决办法]
你的表有点乱,不过还不是很乱
[解决办法]

探讨

SQL code

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 in……

[解决办法]
lz提问题能不能专业一点 看的都急 把源表,目标表,需求写清楚 不要让别人来猜。。。。
[解决办法]
[code=SQL][/code]

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
[解决办法]
个人推荐用coalesce

COALESCE函数说明:
COALESCE (expression_1, expression_2, ...,expression_n)
列表中第一个非空的表达式是函数的返回值,如果所有的表达式都是空值,最终将返回一个空值。

SQL code
with tab_a as(select '1' f1, '2'  f2, NULL f3, NULL f4, '12:00' times from dualunion allselect '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 dualunion allselect 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.timesfrom tab_a ainner join tab_b bon a.times=b.times执行结果:F1 F2 F3 F4 TIMES -- -- -- -- ----- 1  2  12 13 12:00 3  15 4  16 13:00
[解决办法]
這麼沒難度 搞那麼複雜!!!神馬........
[解决办法]
这么多分,我也参与一下!
[解决办法]
SQL code
--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
[解决办法]
使用merge可以一个sql语句完成
SQL code
 merge into a aa      using b bb           on (aa.t=bb.t)       when matched then       update setaa.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); 


[解决办法]
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 ;