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;

再对每行多列合并求和。

mysql 行转列 (结果集以坐标显示)的更多相关文章

  1. MYSQL 行转列 以及基本的聚合函数count,与group by 以及distinct组合使用

    在统计查询中,经常会用到count函数,这里是基础的 MYSQL 行转列 以及基本的聚合函数count,与group by 以及distinct组合使用 -- 创建表 CREATE TABLE `tb ...

  2. MySQL 行转列 -》动态行转列 -》动态行转列带计算

    Pivot Table Using MySQL - A Complete Guide | WebDevZoomhttp://webdevzoom.com/pivot-table-using-mysql ...

  3. mysql行转列、列转行示例

    最近在开发过程中遇到问题,需要将数据库中一张表信息进行行转列操作,再将每列(即每个字段)作为与其他表进行联表查询的字段进行显示. 借此机会,在网上查阅了相关方法,现总结出一种比较简单易懂的方法备用. ...

  4. MySql 行转列 存储过程实现

    同学们在使用mysql的过程中,会遇到一个行转列的问题,就是把多条数据转化成一条数据 用多列显示. 方法1. 实现方式用下面的存储过程,表名对应的修改就行. BEGIN declare current ...

  5. mysql 行转列 列转行

    一.行转列 即将原本同一列下多行的不同内容作为多个字段,输出对应内容. 建表语句 DROP TABLE IF EXISTS tb_score; CREATE TABLE tb_score( id ) ...

  6. [转]mysql 行转列 列转行

    原文地址:http://www.cnblogs.com/xiaoxi/p/7151433.html 一.行转列 即将原本同一列下多行的不同内容作为多个字段,输出对应内容. 建表语句 DROP TABL ...

  7. mysql行转列,函数GROUP_CONCAT(expr)

    demo: 语句: SELECT '行' id, '' product_nameUNIONSELECT id, product_name FROM `product` WHERE id < 5 ...

  8. mysql行转列 问题 SUM(IF(条件,列值,0))

    sum(if(条件,列值,0))语法用例: select name,sum(if(subject="语文",score,0)) as "语文" from gra ...

  9. MySQL行转列、列转行

    一.行转列 有如图所示的表,现在希望查询的结果将行转成列 建表语句如下: CREATE TABLE `TEST_TB_GRADE` ( `ID` int(10) NOT NULL AUTO_INCRE ...

随机推荐

  1. mysql 获取一段时间的数据

    用 sql 获取一段时间内的数据: SELECT * FROM EDI.edi_history WHERE timestampdiff(day, SYSDATE(), create_time_loc) ...

  2. 可视化库-Matplotlib基础设置(第三天)

    1.画一个基本的图 import numpy as np import matplotlib.pyplot as plt # 最基本的一个图,"r--" 线条加颜色, 也可以使用l ...

  3. Python 2.75升级3.6.3

    https://blog.csdn.net/wwwdaan5com/article/details/78218277 Centos 7 默认yum安装python 是2.7.5, (网上看了很多升级都 ...

  4. UNITY C#内存泄漏

    http://www.360doc.com/content/15/0717/09/10504424_485422031.shtml

  5. 7.25 7figting!

    TEXT 82 Proton 马来西亚宝腾汽车 A fork in the road 何去何从?(陈继龙编译) Nov 30th 2006 | HONG KONG From The Economist ...

  6. 【328】Python 控制鼠标/键盘+图片识别 综合应用

    本文是基于 [267]实现跨网络传数据 的基础上的,由于在弹出 putty 之后,需要手动输入命令(pass.sh.get.sh)来实现数据的传递,另外就是处理完之后需要手动关闭 putty,本文解决 ...

  7. jquery中的属性和样式设置

    添加属性 $target.attr({"title":"one piece","name":"solgan"}); 为目 ...

  8. JSONResult引用某博客

    http://www.cnblogs.com/JerryWang1991/archive/2013/03/08/2950457.html 最近开始用MVC做项目,在使用 JsonResult返回数据的 ...

  9. 主线程不能直接调用webservice方法

    @Overrideprotected void onCreate(Bundle savedInstanceState){//StrictMode.setThreadPolicy(new StrictM ...

  10. ANGULAR 2 BITS: UNIFIED DEPENDENCY INJECTION

    Angular 1.x has two APIs for injecting dependencies into a directive. These APIs are not interchange ...