有三个表.
a表
字段:stcd , stnm
例如:
1,q1
2,w2b表
字段
stcd ,idtm(datatime),accp(int)
例如:
1,2005-06-01,20
1,2005-06-01,30
2,2005-06-01,50
2,2005-06-01,1
c表
字段
stcd,MYAVP(int)
例如:
1,40
1,40
2,2
2,50现在要求得到下面的结果,
stcd,stnm,idtm,accp,MYAVP1   ,q1   ,2005-06-01,50,80
2   ,w2   ,2005-06-01,51,52对上面的accp.MYAVP按stcd分组求和!
求该sql 语句.
谢谢

解决方案 »

  1.   

    select 
        a.*,
        b.idtm,
        sum(b.accp) as accp,
        (select sum(MYAVP) from c where stcd=a.stcd) as MYAVP
    from 
        a,b
    where 
        a.stcd=b.stcd 
    group by 
        a.stcd,a.stnm,b.idtm
      

  2.   


    select A.stcd, min(B.idtm) as idtm,sum(B.accp) as accp,sum(C.myavp) as myavp
    from A
          inner join B on A.stcd=B.stcd
          inner join C on A.stcd=C.stcd
    group by A.stcd, A.stnm
    order by A.stcd, A.stnm
      

  3.   

    回复2楼的.只能按stcd 分组求和,不能按group by a.stcd,a.stnm,b.idtm
      

  4.   

    楼主用group by 就行了,表的连接用任何一种方法都行
      

  5.   

    select A.stcd, A.stnm,B.idtm,(select sum(accp) as accp from A,B where A.stcd=B.stcd group by sctd) ,(select sum(MYAVP) as MYAVP from A,C where A.stcd=C.stcd group by sctd) from A,B,C
    where A.stcd=B.stcd and A.stcd=C.stcd
    order by A.stcd
      

  6.   

    楼上的看这个帖子,就知道你错了.
    http://community.csdn.net/Expert/topic/5080/5080966.xml?temp=.8571436