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 ...
随机推荐
- 迷你MVVM框架 avalonjs 1.2.5发布
avalon1.2.5发布,升级ms-widget,整合avalon.require.text到核心,并且修复了avalon.mobile的avalon.innerHTML方法的BUG,让它能执行脚本 ...
- how to deal with EINTR fault
[how to deal with EINTR fault] EINTR:interupted error.是指一个调用被信号给中断,对于同步的耗时调用来说,这个操作常见,譬如select.read. ...
- S EAI 客户主数据导入_test(detail)
一. 客户主数据模板导出 客户主数据和新联系人导入 Account_Config.ini文件 [Public] ConnectString=host="siebel://10.10.1.15 ...
- errorlevel 续1
-------siwuxie095 常用 errorlevel 返回值: backup 0 备份成功 1 未找到备份文件 2 文件共享冲突阻止备份完成 3 用户用 ct ...
- 利用WKWebView实现js与OC交互注意事项
最近在写一些关于wkwebview的一些代码,发现了几点心得,记录一下. 1.js调用OC 我是利用wkwebview进行的开发实现,主要代码有三部分 1.向config注入OC对象 [config. ...
- golang之map数据类型
先上代码…… package main import "fmt" func testMap() { //两种声明map方式,切记,必须初始化才能用,否则panic //var a ...
- Ubuntu 17 Nginx 配置 laravel 运行环境
1 安装 nginx #aptitude install nginx #apatitude install php7.1-fpm 2 在 /etc/nginx/sites-available 建立 s ...
- 电商商品规则属性sku 工具类
/** * 商品模块: * 使用:var SKU = sku.init({ id:'sku', item: item, data:data }); * @config item = [{"i ...
- linux每天一小步---xargs命令详解
1 命令功能 xargs用来从标准输入中执行命令行 xargs命令用来将一些不支持管道传递参数的命令而使之支持 2 命令语法 xargs [选项参数] commands 3 命令参数 -O 当标准输 ...
- swift UITabelVIew - 纯代码自定义tabelViewCell
// // CustomTableViewCell.swift // tab // // Created by su on 15/12/7. // Copyright © 2015年 tian ...