用左连接
select A.a,A.b,sum(B.y)
from a,b
where a.b=b.x(+)
group by A.a,A.b

解决方案 »

  1.   

    不好意思,我写错了,我想得到得是:
    -----------------------------------------
    ID   A.a   A.b       sum(B.y)
    1    aa     bb                      
    2    cc     dd           33
    3    ee     ff           
    4    gg     hh
    5    ff     ll           33请问penitent,是不是把
    select A.a,A.b,sum(B.y)
    from a,b
    where A.ID=B.ID(+)
    group by A.a,A.b
    就可以,还是不用改。
    我先试一下,十分感谢。
      

  2.   

    可以用了,但我在后面加上order by A.a,
    SQL过不了。这个怎么处理?
      

  3.   

    我想,如果使用了group by子句,是可以不使用order by 的,但如果真的需要,应该将order by 放在 group by 的前面.
      

  4.   

    楼上的,应该是在GROUP BY 后ORDER BY
      

  5.   

    to xu_guanghui(小风) 应该将order by 放在 group by 的后面select A.a,A.b,sum(B.y)
    from a,b
    where A.ID=B.ID(+)
    group by A.a,A.b
    order by id;
      

  6.   

    select a.*,b.sum(B.y) from (select id,sum(b.y) from tableB 
                                group by id) b,tableA a 
    where a.id=b.id(+)一定正确,最直观。关键是要有一个左连接,呵呵
      

  7.   

    to littleriver(小江) 如果没有索引,