select col1,count(*) cnt1,sum(decode(col2,1,1,0)) cnt2,null,null
from table1 group by col1
union all
select col1,null,null,count(*) cnt3,sum(decode(col1,1,1,0)) cnt4
from table2 group by col1;

解决方案 »

  1.   

    select col1,count(*) cnt1,sum(decode(col2,1,1,0)) cnt2,null,null
    from table1 group by col1
    union all
    select col1,null,null,count(*) cnt3,sum(decode(col1,1,1,0)) cnt4
    from table2 group by col1;
      

  2.   

    谢谢,但好像不对。结果中的每个col1,其对应的cnt1到cnt4可能都有值,而不是象上面语句那样有cnt1,cnt2的,其cnt3和cnt4就为null.
    而且上面语句执行时也确实出错:ora-01790:expression mus have same datatype as corresponding expression。
    union all前后的语句正常执行,但UNION时出错。
      

  3.   

    select col1,count(*) cnt1,sum(decode(col2,1,1,0)) cnt2,0,0
    from table1 group by col1
    union all
    select col1,0,0,count(*) cnt3,sum(decode(col1,1,1,0)) cnt4
    from table2 group by col1;
      

  4.   

    首先建立一个视图:
    create view col_cnt as
    select col1,count(*) cnt1,sum(decode(col2,1,1,0)) cnt2,0 cnt 3,0 cnt4
    from table1 group by col1
    union all
    select col1,0,0,count(*) cnt3,sum(decode(col1,1,1,0)) cnt4
    from table2 group by col1;然后在用sql语句进行计算。
    select col1,sum(cnt1) cnt1,sum(cnt2) cnt2,sum(cnt3) cnt3,sum(cnt4)cnt4 from col_cnt group by col1;
      

  5.   

    select col1,sum(1) cnt1,sum(decode(a.col2,1,1,0)) cnt2,sum(1) cnt3,sum(decode(b.col2,1,1,0)) cnt4
    from table1 a,table2 b
    where 
    参考这个思路,你的表之间的关系没有说清楚,语句没法写。