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 ...
随机推荐
- JS执行删除前的判断
JS执行删除前如何实现判断. 一. <script> function del(){ if(confirm("确认删除吗")){ alert("yes&quo ...
- leetcode877
public class Solution { public bool StoneGame(int[] piles) { return true; } } 这问题很不好...
- 「小程序JAVA实战」小程序的视频展示页面初始化(63)
转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudeshipinzhanshiyemianchushihua62/ 进 ...
- msxml3.dll 执行页内操作时的错误
msxml3.dll 执行页内操作时的错误 regsvr32 msxml3.dll报错
- 可视化库-Matplotlib-Pandas与sklearn结合(第四天)
1. 计算每一种的比例的百分比 import pandas as pd from matplotlib.ticker import FuncFormatter np.random.seed(0) df ...
- 可视化库-Matplotlib-盒图(第四天)
盒图由五个数值点组成,最小观测值,下四分位数,中位数,上四分位数,最大观测值 IQR = Q3 - Q1 Q3表示上四分位数, Q1表示下四分位数,IQR表示盒图的长度 最小观测值 min =Q1 ...
- for 续7
----------siwuxie095 for 中的变量: FOR 变量参照的替换已被增强.您现在可以使用下列选项语法: ~I - 删除任何引号(" ...
- leetcode 20 Valid Parentheses 有效的括号
描述: 给定一些列括号,判断其有效性,即左括号有对应的有括号,括号种类只为小,中,大括号. 解决: 用栈. bool isValid(string s) { stack<char> st; ...
- UITextFeild银行卡/身份证/电话号任意分割.
日常开发中可能有个需求, 1.银行卡每4位添加一个空格 2.电话号:3 4 4 比如(138 8888 8888)3.身份证(411111 20171213 1314) 看了网上许多方法都是输入的时 ...
- 网页截图API接口,一键自动生成网页截图
背景 最近在开发一个小程序,其中有一个帮助模块,内容为帮助文章列表,文章内容为网站后台编辑的富文本格式.鉴于小程序的特殊性,其对html格式的富文本支持并不友好. 刚开始有人开发了wxparse插件, ...