select in,sum(id*value) as suminvalue,sum((1-id)*value) as sumoutvalue
from (
select 1 as id,table2.in as in,table2.value
from table1,table2
where table1.in=table2.in
union all
select 0 as id,table2.out as in,table2.value
from table1,table2
where table1.in=table2.out
) t0
group by id;

解决方案 »

  1.   

    select table1.in aa,(select sum(value) from table2 where in=aa),(select sum(value) from table2 where out=aa) from table1
      

  2.   

    疑问:table2中没出现的要不要统计?
      

  3.   

    select t1.v_in,sumin,sumout
    from t1,(select sum(value) sumin , t1.v_in
    from t1,t2 
    where t1.v_in=t2.v_in group by t1.v_in) t3,
    (select sum(value) sumout , t1.v_in
    from t1,t2 
    where t1.v_in=t2.v_out group by t1.v_in) t4
    where t1.v_in=t3.v_in and t1.v_in=t4.v_in
      

  4.   

    select table1.in aa,(select sum(value) from table2 where in=aa),(select sum(value) from table2 where out=aa) from table1???
    这句话可以吗
      

  5.   

    select t1.t1in t3in,SumIn.Suminvalue,Sumout.SumOutValue
    FROM table1 t1,
         (SELECT t2In,SUM(t2value) SumInValue FROM table2 GROUP BY t2In)SumIn,
         (SELECT t2Out,SUM(t2value) SumOutValue FROM table2 GROUP BY t2Out)SumOut
    WHERE SumIn.t2In=t1.t1in AND SumOut.t2Out=t1.t1in
      

  6.   

    说明:
    1.上述SQL经过测试(ORACLE 9i);
    2.因为保留字的缘故,对列名作了修改;
      

  7.   

    SELECT a.in,a.sumin,b.sumout 
      FROM 
       (SELECT a.in,sum(b.value) sumin FROM table1 a,table2 b WHERE a.in=b.in GROUP BY a.in)  a,
       (SELECT a.in,sum(b.value) sumout FROM table1 a,table2 b WHERE a.in=b.out GROUP BY a.in) b
     WHERE a.in=b.in