a表
city   1    2    3   4 
开封   20   30   0   50
洛阳   50   0    30  20
许昌   40   60   80  90
b表
city   1    2    3   4
洛阳   50   0    30  20
c表
city   1    2    3   4
开封   20   30   0   50
洛阳   100  0    60  40
许昌   40   60   80  90通过a表和b表得到c表,其中c表中“洛阳”的数据由a表中的“洛阳”的数据和b表中的“洛阳”的数据相加而来
请问这个sql在oracle中怎么写(其中的数字1 2 3 4....代表时间(天),即1号,2号,3号,4号 ....)

解决方案 »

  1.   

    select a.city,a.1+nvl(b.1,0) as 1,a.2+nvl(b.2,0) as 2,a.3+nvl(b.3) as 3,a.4+nvl(b.4) as 4 from a,b where a.city=b.city(+)
      

  2.   

    create table a1(city varchar2(20),c1 number, c2 number ,c3 number, c4 number);
    insert into a1 select '开封' , 20 , 30,  0 , 50 from dual;
    insert into a1 select '洛阳',  50 , 0  ,  30 , 20  from dual;
    insert into a1 select '许昌' , 40 , 60,  80 , 90 from dual;
    commit;
    create table b1(city varchar2(20),c1 number, c2 number ,c3 number, c4 number);
    insert into b1 select '洛阳',  50  ,0 ,   30 , 20 from dual;
    commit;create table c as 
    select a1.city, a1.c1+nvl(b1.c1,0) as c1, a1.c2+nvl(b1.c2,0) as c2, a1.c3+nvl(b1.c3,0) as c3, a1.c4+nvl(b1.c4,0) as c4 from a1
    left join b1 
    on a1.city=b1.city;select * from c;
    -----------------执行结果如下------------------------
      洛阳 100  0  60   40
      许昌 40  60  80   90
      开封 20  30  0   50