billno  indeptno       goodsnum  cbm
123 S394345072 196 18.065
222 S394551479 420 26.142
333 S394551479 68 4.081
333 S394551479 68 17.626
333 S394551479 21 1.26
444 S394551479 121 7.263
444 S394551479 117 18.727
333 S394345072 10 0.919
333 S394345072 10 0.919
    
想按BILLNO号与INDEPTNO号分组合计goodsnum ,cbm。
原则是按BILLNO分组,但相同BILLNO不同indeptno号单独合计预计结果:
billno     indeptno       goodsnum  cbm
123        S394345072       合计数。。      
222        S394551479       合计数。。
333        S394551479       合计数。。
444        S394551479       合计数。。
333        S394345072       合计数。。

解决方案 »

  1.   

    SELECT BILLNO,INDEPTNO,SUM(GOODSNUM),SUM(CBM) FROM TB GROUP BY BILLNO,INDEPTNO
      

  2.   

    select billno,indeptno,sum(goodsnum) goodsnum,sum(cbm) cbm from [Table] group by billno,indeptno
      

  3.   

    select billno,indeptno,sum(goodsnum) cbm
    from tb
    group by billno,indeptno这样?
      

  4.   

    select billno,indeptno,goodsnum = sum(goodsnum),cbm = sum(cbm)
    from tb
    group by billno,indeptno
    order by billno,indeptno
      

  5.   

    select billno,indeptno,sum(goodsnum),sum(cbm) from table group by billno,indeptno
      

  6.   

    --> 测试数据: #T
    if object_id('tempdb.dbo.#T') is not null drop table #T
    create table #T (billno int,indeptno varchar(10),goodsnum int,cbm numeric(5,3))
    insert into #T
    select 123,'S394345072',196,18.065 union all
    select 222,'S394551479',420,26.142 union all
    select 333,'S394551479',68,4.081 union all
    select 333,'S394551479',68,17.626 union all
    select 333,'S394551479',21,1.26 union all
    select 444,'S394551479',121,7.263 union all
    select 444,'S394551479',117,18.727 union all
    select 333,'S394345072',10,0.919 union all
    select 333,'S394345072',10,0.919select billno,indeptno,sum(goodsnum)goodsnum,sum(cbm)cbm from #T group by billno,indeptno/*
    billno      indeptno   goodsnum    cbm
    ----------- ---------- ----------- ---------------------------------------
    123         S394345072 196         18.065
    333         S394345072 20          1.838
    222         S394551479 420         26.142
    333         S394551479 157         22.967
    444         S394551479 238         25.990
    */