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 ) lfrom capacity group by type) cap group by cap.type;
再对每行多列合并求和。
转载于:https://www.cnblogs.com/mryangbo/p/10760796.html
mysql 行转列 (结果集以坐标显示)相关推荐
- mysql行转列和列转行_mysql 行转列和列转行实例详解
mysql行转列.列转行 语句不难,不做多余解释了,看语句时,从内往外一句一句剖析 行转列 有如图所示的表,现在希望查询的结果将行转成列 建表语句如下: create table `test_tb_g ...
- mysql行转列sql函数_sql动态行转列的两种方法
第一种方法: 代码如下: select *from ( select Url,case when Month=01 then '1月' when Month=02 then '2月' when ...
- 5分钟搞懂MySQL - 行转列
小伙伴想精准查找自己想看的MySQL文章?喏 → MySQL专栏目录 | 点击这里 MySQL行转列,对经常处理数据的同学们来说,一定是不陌生的,甚至是印象深刻,因为它大概率困扰过你,让你为之一愣~ ...
- mysql中将列动态转换为行,mysql 行转列 MySQL数据库动态行转列
想把mysql一个表的行转成列,图1是原表,想实现图2的样式SELECT MAX(CAS就是一个动态的行列转换 CREATE TABLE `c_wssb_zz` ( `aa011` varchar(1 ...
- mysql 行转列 显示_mysql 行转列 (结果集以坐标显示)
create table capacity( typeint, numbersint, monthst INT ); select type, sum(case monthst when 1 then ...
- mysql 行转列查询优化_行转列及列转行查询
开发过程中常遇到行转列或是列转行的问题,即需要将数据库中一张表信息进行行转列操作,再将每列(即每个字段)作为与其他表进行联表查询的字段进行显示. 一.行转列:将原来同一列下多行的不同内容作为多个字段, ...
- mysql 行转列分级输出_MySQL如何实现行转列分级输出?_MySQL
概述 好久没写SQL语句,今天看到问答中的一个问题,拿来研究一下. 问题链接:关于Mysql 的分级输出问题 情景简介 学校里面记录成绩,每个人的选课不一样,而且以后会添加课程,所以不需要把所有课程当 ...
- mysql行转列和列转行_Mysql中行转列和列转行
一.行转列 即将原本同一列下多行的不同内容作为多个字段,输出对应内容. 建表语句 DROP TABLE IF EXISTS tb_score; CREATE TABLE tb_score( id IN ...
- mysql 行转列 列转行
一.行转列 即将原本同一列下多行的不同内容作为多个字段,输出对应内容. 建表语句 DROP TABLE IF EXISTS tb_score;CREATE TABLE tb_score(id INT( ...
最新文章
- 【Spring源码】Spring中的AOP底层原理分析
- 奇怪的问题,.Net 2.0发送邮件失败...
- c#中的23种设计模式
- c语言绝对循环,c语言之循环
- rstudio查询命令_Rstudio支持可视化的Markdown编辑了?
- Mybatis源码解析:sql参数处理(1)
- 集训8.16最小生成树讲解
- matlab数学建模试卷,matlab数学建模习题
- “Java Web 程序设计”心得体会
- 元子弹老师-吉他指弹右手技巧
- 仙人掌 圆方树 || 静态 + 动态 (差动态)
- 人力资源管理系统如何促进业务增长
- 学习游戏原画需要什么条件或者基础吗?
- 黑果安装步骤(手把手教你如何安装黑苹果)
- 如何用iMazing软件将苹果手机数据备份及功能详解
- java队名_求JAVA团队名称
- 对象转JSONObject
- 【1】GAN在医学图像上的生成,今如何?
- JavaWeb黑马旅游网-学习笔记10【项目代码】
- (八)Java算法:堆排序(详细图解)
热门文章
- PWN-PRACTICE-BUUCTF-12
- 【CF - 699C】 Vacations (日程安排 dp)
- 【CodeForces - 920E】Connected Components? (dsu,补图连通块,STLset+map,bfs 或bitset)
- 笔试题 - 阶乘问题
- 【牛客 - 315F】美丽的项链(线性dp,递推,我为人人)
- 【 HDU - 5363】Key Set(水题,快速幂,组合数学)
- matlab aviobj,MATLAB AVI 视频读取处理
- php开发地图导航,php 实现百度地图(很详细出来的地图)
- 尼奥机器人不能手机绑定_微信绑定的手机号,如果手机号停用,是不是微信也不能用了?...
- 网络协议分析 | 传输层 :史上最全UDP、TCP协议详解,一篇通~