两个年度的数据库中,都存有项目的数据,我要做一张报表 出来 对比两个年度每个项目的数据情况 以及增减情况。两个年度的项目都要列出来对比,如遇到只有其中一个年度才有的项目 也要列出来,没有这个项目的年度数据就显示为0 具体如下表表1                    表2
项目名称  数据         项目名称   数据
  A        10             A        20
  B        20             C        30
  C        30             E        50
  D        40
结果 :项目名称   数据1  数据2
         A          10     20
         B          20     0
         C          30     30
         D          10     0
         E          0      50两表数据对比,向各位求救

解决方案 »

  1.   

    直接使用val函数,为空时显示0,val(data,0)
      

  2.   

    select col,sum(col2),sum(col3) from 
    (select co1,col2,0 col3 from a
    union all
    select col,0 col2,col3 from b)
    group by co1
      

  3.   


    create table t1(
    project varchar2(20),
    data number
    );
    create table t2(
    project varchar2(20),
    data number
    )insert into t1
    select 'a',10 from dual union all 
    select 'b',20 from dual union all 
    select 'c',30 from dual;insert into t2
    select 'a',20 from dual union all 
    select 'd',20 from dual union all 
    select 'e',30 from dual;select decode(t1.project,null,t2.project,t1.project) as project,
    nvl(t1.data,0),
    nvl(t2.data,0)
    from t1 full join t2
    on t1.project=t2.project;
      

  4.   

    select project,nvl(cn,0) from (
      select distinct a.project from a
      union 
      select distinct b.project from b) t1
    left join (
      select a.project ,count(*) cn from a) t2 on t1.project=t2.project
    left join (
      select b.project ,count(*) from b) t3 on t1.project=t3.project
      

  5.   

    full join + nvl 可以实现、