select a.code, sum(inmoney),sum(outmoney),min(qyname),kind  
from a,b,c,d
where  b.code = a.code(+) and b.code = c.code(+) and a.qycode = d.qycode
group by kind , a.code

解决方案 »

  1.   

    a:code  inmoney   qycode
        1       10      0001
        1       20      0002
        1       30      0001
    从这个表可以看出,同一个code对应了多个qycode也就是多个qyname
    如果楼主
    “我想列出code,sum(inmoney),sum(outmoney),qyname,kind 按照大类(kind)和代码(code)分组得出上述数据列表,”
    必然会用到qyname的分组,否则无法取qyname的值。不知道是不是楼主的要求有误
      

  2.   

    例如上述数据 我想得到如下结果:
     code     sum(inmoney)   sum(outmoney)  qyname  kind
      1         10+30          1000+2000      张     1
      1           20           1000+2000      王     1
      2         10+20             1000        陈     1
      3                           1500               2
      

  3.   

    问题2:
    b: code  outmoney
        1      1000
        1      2000
        2      1000
        3      1500
    这个表和qyname一点关系也没有,如何处理
      

  4.   

    有点怪啊
    sum(outmoney)  
    1000+2000   
    1000+2000
      

  5.   

    b表是和qyname没有关系 我只是想把他们一起列出来
      

  6.   

    感觉表设计的有点问题b表是和qyname没有关系 我只是想把他们一起列出来没有关系怎么一起列出来?还要分组
      

  7.   

    如果其它要求不变,但不需要按照qyname分组呢,谢谢!
      

  8.   

    select t.code,sum(t.inm) as inall,sum(t.outm) as outall,t.qyname,c.kind
    from
    (
    select b.code,isnull(a.inmoney,0) as inm,isnull(b.outmoney,0) as outm ,isnull(d.qyname,'') as qyname
    from a right outer join b  
    on a.code=b.code
    left outer join d
    on a.qycode=d.qycode
    ) t
    left outer join c
    on t.code=c.code
    group by t.code,t.qyname,c.kind--结果
    code        inall       outall      qyname     kind        
    ----------- ----------- ----------- ---------- ----------- 
    1           40          3000        wang       1
    1           80          6000        zhang      1
    2           30          2000        chen       1
    3           0           1500                   2(4 row(s) affected)
      

  9.   

    以上我用sql server写的,不过用的SQL-92的写法,在oracle中应该也可以用,看看是不是符合搂主的要求,呵呵
      

  10.   

    不对,结果还是不对,a和b的关系不对,a中没有code=3的数据,不能join表设计的有问题,a和b应该合在一张表里的,这样就很容易了,不会有上面的问题
      

  11.   

    NinGoo(宁哥)说的对.
    楼主先修改一下表吧
      

  12.   

    宁哥你好,你输出的结果的第二行好像和我想的不是一样的(inall和outall),我想象的应该分别为
    1           20           1000+2000      王     1
      

  13.   

    如果d表不要,a表中不要qycode字段,要按照code分组求sum(inmoney)和sum(outmoney)该怎么写呢?
      

  14.   

    Oracle 环境中,表结构与题同!执行:
    select t.code code,(select sum(inmoney) from a where code=t.code and qycode=t.qycode) suminmoney,(select sum(outmoney) from b where code=t.code) sumoutmoney,(select qyname from d where qycode=t.qycode) qyname,(select kind from c where code=t.code) kind from (select b.code,a.qycode from a,b where a.code(+)=b.code group by b.code,a.qycode) t;
    结果如下:
          CODE SUMINMONEY SUMOUTMONEY QYNAME                     KIND
    ---------- ---------- ----------- -------------------- ----------
             1         40        3000 张                            1
             1         20        3000 王                            1
             2         30        1000 陈                            1
             3                   1500                               2
      

  15.   

    select s.code,s.inmoney "sum(inmoney)",t.outmoney "sum(outmoney)",s.qyname,s.kind from
      (
        select c.code,x.inmoney,x.qyname,c.kind from c,
          (
          select a.qycode,a.code,a.inmoney,d.qyname from
            (
              select code,qycode,sum(inmoney) inmoney from a
                group by code,qycode
            ) a,d
            where a.qycode=d.qycode
          ) x
          where c.code=x.code(+)
      ) s,
      (
        select code,sum(outmoney) outmoney from b
          group by code
      ) t
      where s.code=t.code;