zoukankan      html  css  js  c++  java
  • SQL分类汇总统计聚合查询

    这几天在做某市总工会的一个捐赠系统,需要做一个分类汇总。

    先叙述下表结构

    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 )
  • 相关阅读:
    7月17日
    7月16日学习记录
    7月15日学习记录
    git 学习
    投稿相关
    ubuntu16.04 安装以及要做的事情
    python学习使用
    图像相关
    不识别移动硬盘
    深度学习
  • 原文地址:https://www.cnblogs.com/MichaelZhangX/p/2694544.html
Copyright © 2011-2022 走看看