SQL查询一批数据,怎么在最后一行增加个合计

来源:互联网 发布:网络教育专升本报名 编辑:程序博客网 时间:2024/05/21 20:22

1.示例


注解:nullrollup产生的汇总项


2.yf字段进行处理就可以显示“合计”


注解:但是要显示的字段类型必须与yf字段类型一致,若yfint,就不能显示varchar类型


3.显示的字段类型必须要与查询结果字段类型一致



示例代码:select      case when yf is not null then yf else '合计' end yf,     sum(cast(bqbj1 as money)) bjhz,     sum(cast(bqll1 as money)) lxhz,     sum(cast(bqwh as money)) whhzfrom (select SUBSTRING(rq,6,2) yf,bqbj1,bqll1,bqwh      from formtable_main_84_dt1 d       where rq between '2017-07-01' and '2017-09-30') as agroup by yfwith rollup




原创粉丝点击