sql分组求和

需求是:一个月有多个发布額,现在要求按月统计发布額,例如:1月发布額--35900,2月发布額--2300

sql 语句如下:

--按月分组,求和

select dMonth,CodeName,sum(iAmount) from

 (

--求出全部日期,发布額 
SELECT convert(varchar(7),a.dRegDate,120) as dMonth,a.cOper_Code+'-'+w.work_Name as CodeName
,iAmount as iAmount
FROM viw_ExecuteAll A
INNER JOIN tbl_WorkSub S ON LEFT(A.cOper_Code, 2) = S.Work_group
inner join tbl_dealer d on a.cCorp_code = d.cDealer_code
inner join tbl_Work w on w.work_code = a.cOper_Code
WHERE a.iAmount >0 and s.cMemo =1 and bBig =0 and (dFrom_Date <= '2007-07-30') AND
(dTo_Date >= '2007-06-01') and s.cCity_Name ='北京' and d.cYyZzCode ='1'
) aa

group by dMonth,CodeName

posted @ 2008-08-20 15:18  艾伦  阅读(6925)  评论(0编辑  收藏  举报