【mysql练习】转置,总计,分组
1,有如下一个表,要求把这个表按照2016,2017,2018,2019,把从1月到12月的金额展示出来。
profit表:
year month amount
2019 1 5
2019 3 6
2018 3 7
2018 4 8
2018 5 9
2017 1 10
2017 2 11
2016 3 12
展现成:
year,1,2,3,4,5,6,7,8,9,10,11,12
2016,0,0,12,0,0,0,0,0,0,0,0,0
2017,10,11,0,0,0,0,0,0,0,0,0,0
2018,0,0,7,8,9,0,0,0,0,0,0,0
2019,5,0,6,0,0,0,0,0,0,0,0,0
解析:由于要展示1-12月的数据,所以先把1-12月的列列出来。每列的值都由查询对应月份的结果进行展示,4年的结果分组展示。
第一步,写出每个月的查询结果:
select amount as "1月" from profit where month="1";
select amount as "2月" from profit where month="2";
select amount as "3月" from profit where month="3";
select amount as "4月" from profit where month="4";
select amount as "5月" from profit where month="5";
select amount as "6月" from profit where month="6";
select amount as "7月" from profit where month="7";
select amount as "8月" from profit where month="8";
select amount as "9月" from profit where month="9";
select amount as "10月" from profit where month="10";
select amount as "11月" from profit where month="11";
select amount as "12月" from profit where month="12";
第二步:
将每个月的查询结果作为子查询,通过year关联
select year,
(select amount from profit m1 where m1.month="1" and m1.year=m.year) as m1,
(select amount from profit m2 where m2.month="2" and m2.year=m.year) as m2,
(select amount from profit m3 where m3.month="3" and m3.year=m.year) as m3,
(select amount from profit m4 where m4.month="4" and m4.year=m.year) as m4,
(select amount from profit m5 where m5.month="5" and m5.year=m.year) as m5,
(select amount from profit m6 where m6.month="6" and m6.year=m.year) as m6,
(select amount from profit m7 where m7.month="7" and m7.year=m.year) as m7,
(select amount from profit m8 where m8.month="8" and m8.year=m.year) as m8,
(select amount from profit m9 where m9.month="9" and m9.year=m.year) as m9,
(select amount from profit m10 where m10.month="10" and m10.year=m.year) as m10,
(select amount from profit m11 where m11.month="11" and m11.year=m.year) as m11,
(select amount from profit m12 where m12.month="12" and m12.year=m.year) as m12
from profit m group by m.year
第三步,处理Null
select year,
IFNULL((select amount from profit m1 where m1.month="1" and m1.year=m.year),0) as m1,
IFNULL((select amount from profit m2 where m2.month="2" and m2.year=m.year),0) as m2,
IFNULL((select amount from profit m3 where m3.month="3" and m3.year=m.year),0) as m3,
IFNULL((select amount from profit m4 where m4.month="4" and m4.year=m.year),0) as m4,
IFNULL((select amount from profit m5 where m5.month="5" and m5.year=m.year),0) as m5,
IFNULL((select amount from profit m6 where m6.month="6" and m6.year=m.year),0) as m6,
IFNULL((select amount from profit m7 where m7.month="7" and m7.year=m.year),0) as m7,
IFNULL((select amount from profit m8 where m8.month="8" and m8.year=m.year),0) as m8,
IFNULL((select amount from profit m9 where m9.month="9" and m9.year=m.year),0) as m9,
IFNULL((select amount from profit m10 where m10.month="10" and m10.year=m.year),0) as m10,
IFNULL((select amount from profit m11 where m11.month="11" and m11.year=m.year),0) as m11,
IFNULL((select amount from profit m12 where m12.month="12" and m12.year=m.year),0) as m12
from profit m group by m.year
结果:
增加统计当年amount
select year,sum(amount) as "当年总金额",
IFNULL((select amount from profit m1 where m1.month="1" and m1.year=m.year),0) as m1,
IFNULL((select amount from profit m2 where m2.month="2" and m2.year=m.year),0) as m2,
IFNULL((select amount from profit m3 where m3.month="3" and m3.year=m.year),0) as m3,
IFNULL((select amount from profit m4 where m4.month="4" and m4.year=m.year),0) as m4,
IFNULL((select amount from profit m5 where m5.month="5" and m5.year=m.year),0) as m5,
IFNULL((select amount from profit m6 where m6.month="6" and m6.year=m.year),0) as m6,
IFNULL((select amount from profit m7 where m7.month="7" and m7.year=m.year),0) as m7,
IFNULL((select amount from profit m8 where m8.month="8" and m8.year=m.year),0) as m8,
IFNULL((select amount from profit m9 where m9.month="9" and m9.year=m.year),0) as m9,
IFNULL((select amount from profit m10 where m10.month="10" and m10.year=m.year),0) as m10,
IFNULL((select amount from profit m11 where m11.month="11" and m11.year=m.year),0) as m11,
IFNULL((select amount from profit m12 where m12.month="12" and m12.year=m.year),0) as m12
from profit m group by m.year;
增加统计4年总金额:
select year,sum(amount) as "年度总金额",
IFNULL((select amount from profit m1 where m1.month="1" and m1.year=m.year),0) as m1,
IFNULL((select amount from profit m2 where m2.month="2" and m2.year=m.year),0) as m2,
IFNULL((select amount from profit m3 where m3.month="3" and m3.year=m.year),0) as m3,
IFNULL((select amount from profit m4 where m4.month="4" and m4.year=m.year),0) as m4,
IFNULL((select amount from profit m5 where m5.month="5" and m5.year=m.year),0) as m5,
IFNULL((select amount from profit m6 where m6.month="6" and m6.year=m.year),0) as m6,
IFNULL((select amount from profit m7 where m7.month="7" and m7.year=m.year),0) as m7,
IFNULL((select amount from profit m8 where m8.month="8" and m8.year=m.year),0) as m8,
IFNULL((select amount from profit m9 where m9.month="9" and m9.year=m.year),0) as m9,
IFNULL((select amount from profit m10 where m10.month="10" and m10.year=m.year),0) as m10,
IFNULL((select amount from profit m11 where m11.month="11" and m11.year=m.year),0) as m11,
IFNULL((select amount from profit m12 where m12.month="12" and m12.year=m.year),0) as m12
from profit m group by m.year
UNION ALL
select * from
(select '总计',sum(t2.`年度总金额`),sum(m1),sum(m2),sum(m3),sum(m4),sum(m5),sum(m6),sum(m7),sum(m8),sum(m9),sum(m10),sum(m11),sum(m12) from
(
select year,sum(amount) as "年度总金额",
IFNULL((select amount from profit m1 where m1.month="1" and m1.year=m.year),0) as m1,
IFNULL((select amount from profit m2 where m2.month="2" and m2.year=m.year),0) as m2,
IFNULL((select amount from profit m3 where m3.month="3" and m3.year=m.year),0) as m3,
IFNULL((select amount from profit m4 where m4.month="4" and m4.year=m.year),0) as m4,
IFNULL((select amount from profit m5 where m5.month="5" and m5.year=m.year),0) as m5,
IFNULL((select amount from profit m6 where m6.month="6" and m6.year=m.year),0) as m6,
IFNULL((select amount from profit m7 where m7.month="7" and m7.year=m.year),0) as m7,
IFNULL((select amount from profit m8 where m8.month="8" and m8.year=m.year),0) as m8,
IFNULL((select amount from profit m9 where m9.month="9" and m9.year=m.year),0) as m9,
IFNULL((select amount from profit m10 where m10.month="10" and m10.year=m.year),0) as m10,
IFNULL((select amount from profit m11 where m11.month="11" and m11.year=m.year),0) as m11,
IFNULL((select amount from profit m12 where m12.month="12" and m12.year=m.year),0) as m12
from profit m group by m.year
) t2 ) t3
改进版:
select IFNULL(year,"总计") as year,sum(t.total) as 'total' ,sum(t.m1) as '1月',
sum(t.m2) as '2月',
sum(t.m3) as '3月',
sum(t.m4) as '4月',
sum(t.m5) as '5月',
sum(t.m6) as '6月',
sum(t.m7) as '7月',
sum(t.m8) as '8月',
sum(t.m9) as '9月',
sum(t.m10) as '10月',
sum(t.m11) as '11月',
sum(t.m12) as '12月'from
(
select year,sum(amount) as 'total',
IFNULL((select amount from profit m1 where m1.month="1" and m1.year=m.year),0) as m1,
IFNULL((select amount from profit m2 where m2.month="2" and m2.year=m.year),0) as m2,
IFNULL((select amount from profit m3 where m3.month="3" and m3.year=m.year),0) as m3,
IFNULL((select amount from profit m4 where m4.month="4" and m4.year=m.year),0) as m4,
IFNULL((select amount from profit m5 where m5.month="5" and m5.year=m.year),0) as m5,
IFNULL((select amount from profit m6 where m6.month="6" and m6.year=m.year),0) as m6,
IFNULL((select amount from profit m7 where m7.month="7" and m7.year=m.year),0) as m7,
IFNULL((select amount from profit m8 where m8.month="8" and m8.year=m.year),0) as m8,
IFNULL((select amount from profit m9 where m9.month="9" and m9.year=m.year),0) as m9,
IFNULL((select amount from profit m10 where m10.month="10" and m10.year=m.year),0) as m10,
IFNULL((select amount from profit m11 where m11.month="11" and m11.year=m.year),0) as m11,
IFNULL((select amount from profit m12 where m12.month="12" and m12.year=m.year),0) as m12
from profit m group by m.year
) t
group by year with rollup;
【mysql练习】转置,总计,分组的更多相关文章
- 如何在MySQL中查询每个分组的前几名【转】
问题 在工作中常会遇到将数据分组排序的问题,如在考试成绩中,找出每个班级的前五名等. 在orcale等数据库中可以使用partition语句来解决,但在mysql中就比较麻烦了.这次翻译的文章就是专门 ...
- 【mybatis】【mysql】mybatis查询mysql,group by分组查询报错:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column
mybatis查询mysql,group by分组查询报错:Expression #1 of SELECT list is not in GROUP BY clause and contains no ...
- mysql使用GROUP BY分组实现取前N条记录的方法
MySQL中GROUP BY分组取前N条记录实现 mysql分组,取记录 GROUP BY之后如何取每组的前两位下面我来讲述mysql中GROUP BY分组取前N条记录实现方法. 这是测试表(也不知道 ...
- mysql中的过滤分组
本文节选自<MYSQL必知必会> 一. 过滤分组 除了能用GROUP BY分组数据外,MySQL还允许过滤分组,规定包括哪些分组,排除哪些分组.例如,可能想要列出至少有两个订单的所有顾客. ...
- 快速回顾MySQL:汇总和分组
10.3 汇总数据 我们经常需要汇总数据而不用把它们实际检索处出来,为此MySQL提供了专门的函数.使用这些函数,MySQL查询可用于检索数据,以便分析和报表的生成.这种类型的检索例子有以下几种: 确 ...
- MySQL之排序、分组(五)
一.排序 格式:select * from 表 order by 字段 asc|desc 1.查询所有的商品进行排序(升序asc.降序desc) mysql> select * from pro ...
- 如何在mysql中查询每个分组的前几名
问题 在工作中常会遇到将数据分组排序的问题,如在考试成绩中,找出每个班级的前五名等. 在orcale等数据库中可以使用partition 语句来解决,但在MySQL中就比较麻烦了.这次翻译的文章就是 ...
- MySQL之——GROUP BY分组取字段最大值
转载自:http://blog.csdn.net/l1028386804/article/details/54657412 假设有一个业务场景,需要查询用户登录记录信息,其中表结构如下: CREATE ...
- MySQL数据中分级分组显示数据
前面已经有了SqlServer数据分级分组显示数据了.今天又来做一个MySQL数据库中的分级分组显示,SqlServer中用到了递归,这里为了简单就直接把根的数据显示为0 ,而不用递归了. 在MySQ ...
- 浅析MySQL使用 GROUP BY 分组聚合与细分聚合
原创文章,转载请注明出处:http://www.cnblogs.com/weix-l/p/7521278.html: 若有错误,请评论指出,谢谢! 1. 聚合函数(Aggregate Function ...
随机推荐
- Gitlab Ubuntu部署
一.安装存储库 sudo curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh ...
- 关于ie浏览器query ajax提交单个操作无效
第一次写博客 大家不要喷我!!!! 需求需要开发一个无刷新的用户注销和恢复注销功能 遇到的实际问题直接贴图----> 这是开始页面 当点击红xx时提示修改成功 这里似乎是对的哈 但是等点击刷新的 ...
- 安装MinGW-C++开发环境2--软件安装
下面以MinGW安装路径为C:\Local\MinGW64为例说明安装过程: 1.解压x86_64-8.1.0-release-posix-seh-rt_v6-rev0.7z到C:\Local\Min ...
- Pytorch实战学习(三):多维输入
<PyTorch深度学习实践>完结合集_哔哩哔哩_bilibili Multiple Dimension Imput 1.糖尿病预测案例 2.输入8个特征变量 3.Mini-batch N ...
- Python 堆、栈和队列详解
队列: 1.队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表.进行插入操作的端称为队尾,进 ...
- kafka删除topic清空数据
一般情况下,是不会删除数据的.到达一定时间后,kafka会自动删除.如果一定要删除可以删除topic在重建topic了 No. 1: 如果需要被删除topic 此时正在被程序 produce和cons ...
- robots.txt 文件说明
robots其实就是指Robots协议,Robots协议(也称为爬虫协议.机器人协议等)的全称是"网络爬虫排除标准"(Robots Exclusion Protocol),网站通过 ...
- mysql企业常用集群架构
转自 https://blog.csdn.net/kingice1014/article/details/76020061 1.mysql企业常用集群架构 在中小型互联网的企业中.mysql的集群一般 ...
- 斐讯K2_V22.6.507.43降级+刷机整个过程
K2刷机整个过程 (包括降级) 开始之前请先下载好相对应的工具包,其中包括: 官方固件:"K2_V22.6.506.28.bin"."K2_V22.6.507.43.bi ...
- 基于Sobel算子的图像边缘检测
索贝尔算子(Sobeloperator)主要用于获得数字图像的一阶梯度,是一种离散性差分算子.它是prewitt算子的改进形式,改进之处在于sobel算子认为,邻域的像素对当前像素产生的影响不是等价的 ...