mysql数据行转列

在你找工作的经历当中,是否有面试官问过你:数据库行转列如何实现? 
一个典型的例子如下: 
    有这样一个订单表(om_order)一条记录包含订单号、创建日期、订单总金额; 
让你统计不同年份对应各月份的销售情况,要求每一年的销售情况一行展示,效果如下:

+--------+-------------+--------+----------+----------+----------+----------+----------+----------+----------+----------+-----------+--------------+ 
| 年份   | 一月        | 二月   | 三月     | 四月     | 五月     | 六月     | 七月     | 八月     | 九月     | 十月     | 十一月    | 十二月       | 
+--------+-------------+--------+----------+----------+----------+----------+----------+----------+----------+----------+-----------+--------------+ 
|   2011 |        0.00 |   0.00 |     0.00 |     0.00 |     0.00 |     0.00 |     0.00 |     0.00 |     0.00 |     0.00 |      0.00 | 157791865.93 | 
|   2012 | 59793317.04 |   0.00 | 79342.20 | 38757.74 | 45519.50 | 24669.33 | 49387.64 | 23206.87 | 10314.14 | 26005.00 |  60958.59 |      6386.90 | 
+--------+-------------+--------+----------+----------+----------+----------+----------+----------+----------+----------+-----------+--------------+ 

// 表结构如下: 
create table if not exists om_order( 
Order_No int primary key, 
Create_Date datetime, 
Order_Sum decimal(18,2) 
); 

好了,如何实现想要的效果呢? 
要一步实现确实很有点困难,我们一步一步分析,先不考虑一行展示所有月份数据,我们先按照常规统计(列的形式)来展示每月销售情况,sql如下: 

select year(o.`Create_Date`) as '年份',month(o.`Create_Date`) as '月份',sum(`Order_Sum`) as '订单销售额' from `om_order` o 
-- where year(o.`Create_Date`) = '2012' 
group by year(o.`Create_Date`),month(o.`Create_Date`); 

有了这样的数据后结果后在处理就是单纯的行转列了,sql如下: 
select tmp.o_year as '年份', 
       sum(case when tmp.o_month = 1 then tmp.o_total else 0 end) as '一月', 
       sum(case when tmp.o_month = 2 then tmp.o_total else 0 end) as '二月', 
       sum(case when tmp.o_month = 3 then tmp.o_total else 0 end) as '三月', 
       sum(case when tmp.o_month = 4 then tmp.o_total else 0 end) as '四月', 
       sum(case when tmp.o_month = 5 then tmp.o_total else 0 end) as '五月', 
       sum(case when tmp.o_month = 6 then tmp.o_total else 0 end) as '六月', 
       sum(case when tmp.o_month = 7 then tmp.o_total else 0 end) as '七月', 
       sum(case when tmp.o_month = 8 then tmp.o_total else 0 end) as '八月', 
       sum(case when tmp.o_month = 9 then tmp.o_total else 0 end) as '九月', 
       sum(case when tmp.o_month = 10 then tmp.o_total else 0 end) as '十月', 
       sum(case when tmp.o_month = 11 then tmp.o_total else 0 end) as '十一月', 
       sum(case when tmp.o_month = 12 then tmp.o_total else 0 end) as '十二月'/*, 
       sum(tmp.o_total) as '总计'*/ 
from     
(       
select year(o.`Create_Date`) as o_year,month(o.`Create_Date`) as o_month,sum(`Order_Sum`) as o_total from `om_order` o 
group by year(o.`Create_Date`),month(o.`Create_Date`) 
) tmp 
group by tmp.o_year;

好了,大功告成!

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

KVM调整cpu和内存

一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid