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 合计数。。
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 合计数。。
from tb
group by billno,indeptno这样?
from tb
group by billno,indeptno
order by billno,indeptno
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
*/