create table capacity(
type int ,
numbers int ,
monthst INT
);

select type,
sum(case monthst when 1 then numbers else 0 end ) 一月,
sum(case monthst when 2 then numbers else 0 end ) 二月,
sum(case monthst when 3 then numbers else 0 end ) 三月,
sum(case monthst when 4 then numbers else 0 end ) 四月,
sum(case monthst when 5 then numbers else 0 end ) 五月,
sum(case monthst when 6 then numbers else 0 end ) 六月,
sum(case monthst when 7 then numbers else 0 end ) 七月,
sum(case monthst when 8 then numbers else 0 end ) 八月,
sum(case monthst when 9 then numbers else 0 end ) 九月,
sum(case monthst when 10 then numbers else 0 end ) 十月,
sum(case monthst when 11 then numbers else 0 end ) 十一月,
sum(case monthst when 12 then numbers else 0 end ) 十二月
from capacity group by type;

按type分组,并对每月的对应type的numbers求和。

select cap.type,sum(cap.a+cap.b+cap.c) 一季度,sum(cap.d+cap.e+cap.f) 二季度,sum(cap.g+cap.h+cap.i) 三季度,sum(cap.j+cap.k+cap.l) 四季度 from
(select type,
sum(case monthst when 1 then numbers else 0 end ) a,
sum(case monthst when 2 then numbers else 0 end ) b,
sum(case monthst when 3 then numbers else 0 end ) c,
sum(case monthst when 4 then numbers else 0 end ) d,
sum(case monthst when 5 then numbers else 0 end ) e,
sum(case monthst when 6 then numbers else 0 end ) f,
sum(case monthst when 7 then numbers else 0 end ) g,
sum(case monthst when 8 then numbers else 0 end ) h,
sum(case monthst when 9 then numbers else 0 end ) i,
sum(case monthst when 10 then numbers else 0 end ) j,
sum(case monthst when 11 then numbers else 0 end ) k,
sum(case monthst when 12 then numbers else 0 end ) l
from capacity group by type) cap
group by cap.type;

再对每行多列合并求和。

最新文章

  1. 冰冻三尺非一日之寒--web框架Django(翻页、cookie)
  2. 上传图片预览JS脚本 Input file图片预览的实现示例
  3. Visual Studio中安装viemu后,vim vax 快捷键大全
  4. angular2 - content projection-
  5. mvn filter autoconfig 产生自动配置
  6. SQL笔记 - CTE递归实例(续):显示指定部门的全称
  7. C#中文和UNICODE编码转换
  8. php字符串比较函数
  9. Openfire服务器MySQL优化
  10. javascript GB2312转UTF8
  11. My-sql #1045 - Access denied for user 'root'@'localhost' (using password: NO)
  12. VISUAL STUDIO 2005连接MYSQL数据库
  13. Qt编译慢吗?
  14. 在Java中怎样逐行地写文件?
  15. Spring Boot实战:静态资源处理
  16. netty之NioEventLoopGroup源码分析二
  17. Xamarin 学习笔记 - Page(页面)
  18. mybatis配置与使用
  19. HTTP 学习心得
  20. 这可能是最详细的Python文件操作

热门文章

  1. 「小程序JAVA实战」小程序的横向视频和页面拦截(59)
  2. iOS开发者有价值的工具集
  3. jsp的静态导入
  4. docker swarm test
  5. Excel VBA入门(九)操作工作薄
  6. 学习 java 编程语言两个月来的感受
  7. sql产生随机数字
  8. hive分隔符总结
  9. Laravel 使用 Provider 为程序提供运行时配置服务
  10. Homestead window10 storage:link 不能建立符号链接的处理办法