这几天在做某市总工会的一个捐赠系统,需要做一个分类汇总。
先叙述下表结构
id | DonTime | DonType | DonMoney | …………
DonType 分为三种类型:社会个人捐款、单位捐款、单位个人捐款
现在的需求是按照每日,分类汇总三种类别的捐赠金额总和及捐赠次数
花了点时间写了一下sql查询语句,在此记录下,以备日后再用。
select convert ( char( 10 ),DonTime , 120) as DonDate , sum( DonMoney )as jz_all , sum (case dontype when ' 社会个人捐款 ' then DonMoney else 0 end) as jz_shgr, count (case dontype when ' 社会个人捐款 ' then 1 else null end ) as jz_shgr_num, sum (case dontype when ' 单位捐款' then DonMoney else 0 end ) as jz_dw , count (case dontype when ' 单位捐款' then DonMoney else null end) as jz_dw_num, sum (case dontype when ' 单位个人捐款 ' then DonMoney else 0 end) as jz_dwgr, count (case dontype when ' 单位个人捐款 ' then DonMoney else null end ) as jz_dwgr from DonationInfo where 1= 1 and DATEDIFF ( day, DonTime ,'2011-01-01' ) <= 0 and DATEDIFF ( day, DonTime ,'2011-02-04' ) >= 0 group by convert( char (10 ), DonTime, 120 )