mysql 行转列 (结果集以坐标显示)
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 行转列 (结果集以坐标显示)的更多相关文章
- MYSQL 行转列 以及基本的聚合函数count,与group by 以及distinct组合使用
在统计查询中,经常会用到count函数,这里是基础的 MYSQL 行转列 以及基本的聚合函数count,与group by 以及distinct组合使用 -- 创建表 CREATE TABLE `tb ...
- MySQL 行转列 -》动态行转列 -》动态行转列带计算
Pivot Table Using MySQL - A Complete Guide | WebDevZoomhttp://webdevzoom.com/pivot-table-using-mysql ...
- mysql行转列、列转行示例
最近在开发过程中遇到问题,需要将数据库中一张表信息进行行转列操作,再将每列(即每个字段)作为与其他表进行联表查询的字段进行显示. 借此机会,在网上查阅了相关方法,现总结出一种比较简单易懂的方法备用. ...
- MySql 行转列 存储过程实现
同学们在使用mysql的过程中,会遇到一个行转列的问题,就是把多条数据转化成一条数据 用多列显示. 方法1. 实现方式用下面的存储过程,表名对应的修改就行. BEGIN declare current ...
- mysql 行转列 列转行
一.行转列 即将原本同一列下多行的不同内容作为多个字段,输出对应内容. 建表语句 DROP TABLE IF EXISTS tb_score; CREATE TABLE tb_score( id ) ...
- [转]mysql 行转列 列转行
原文地址:http://www.cnblogs.com/xiaoxi/p/7151433.html 一.行转列 即将原本同一列下多行的不同内容作为多个字段,输出对应内容. 建表语句 DROP TABL ...
- mysql行转列,函数GROUP_CONCAT(expr)
demo: 语句: SELECT '行' id, '' product_nameUNIONSELECT id, product_name FROM `product` WHERE id < 5 ...
- mysql行转列 问题 SUM(IF(条件,列值,0))
sum(if(条件,列值,0))语法用例: select name,sum(if(subject="语文",score,0)) as "语文" from gra ...
- MySQL行转列、列转行
一.行转列 有如图所示的表,现在希望查询的结果将行转成列 建表语句如下: CREATE TABLE `TEST_TB_GRADE` ( `ID` int(10) NOT NULL AUTO_INCRE ...
随机推荐
- spring quartz 配置及说明
方式一,jobDetail的bean利用MethodInvokingJobDetailFactoryBean 工厂包装 : ()定义一个bean,执行具体的业务操作. <bean id=&quo ...
- Ubuntu12.04编译vlc-android详细流程
作者:wainiwann 出处:http://www.cnblogs.com/wainiwann/ 本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则 ...
- 用logger在控制台打印信息
第一步: 导入jar包,maven项目可以直接添加 <dependency> <groupId>log4j</groupId> <artifactId> ...
- 【转】C# 调用 C++ 数据转换
原文:https://www.cnblogs.com/82767136/articles/2517457.html 在合作开发时,C#时常需要调用C++DLL,当传递参数时时常遇到问题,尤其是传递和返 ...
- 基础知识 一个工具给win7 win10的同学 或者MAC 可以跳过
- ROS标定IDS相机
参考 ROS 相机标定http://blog.csdn.net/ArtistA/article/details/51125560 ROS里的标定程序只要使用了OPNCV的标定程序: opencv 相机 ...
- [z] How can we render CSS3 in a WebBrowser Control ?
http://www.pedautreppe.com/post/How-can-we-render-CSS3-in-a-WebBrowser-Control-.aspx
- 界面编程与视图(View)组件
1.视图组件与容器组件 Android应用绝大部分UI组件都放在Android.widget包及其子包.android.view包及其子包中,其所有UI组件都继承了view类,view组件代表一个空白 ...
- sqlserver2012 清除日志
1. backup log wwgl_demo to disk='D:\DATA_BACKUP\2017-07-19.log' 2. 右键数据库-->任务-->收缩-->文件 ...
- curl:get,post 以及SoapClien访问webservice数据
一.curl get模式 public function close_order(){ $url="http://192.168.2.50:7777/U8API.asmx?op=Insert ...