温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySQL DML操作--------实现pivot行转列功能最佳实战

发布时间:2020-07-14 01:47:15 阅读:8111 作者:asd1123509133 栏目:MySQL数据库
亿速云mysql数据库,读写分离,安全稳定,弹性扩容,低至0.3元/天!! 点击查看>>

1. 背景

  * 由于MySQL 不支持类型Oracle与SQL Server的pivot功能进行行列转换。

MySQL DML操作--------实现pivot行转列功能最佳实战

2. 表与数据

mysql> select * from t_temp;
+---------+-----------+------------+
| year    | season    | orderCount |
+---------+-----------+------------+
| 2010年  | 一季度    |        100 |
| 2010年  | 二季度    |        200 |
| 2010年  | 三季度    |        300 |
| 2010年  | 四季度    |        400 |
| 2011年  | 一季度    |        150 |
| 2011年  | 二季度    |        300 |
| 2011年  | 三季度    |        450 |
| 2011年  | 四季度    |        600 |
+---------+-----------+------------+
8 rows in set (0.00 sec)

3. 通过子查询与case when判断实现

mysql> select yearsum(orderCount1) '第一季度', 
    ->              sum(orderCount2) '第二季度', 
    ->              sum(orderCount3) '第三季度', 
    ->              sum(orderCount4) '第四季度' 
    -> from  
    ->     (
    ->         select year, 
    ->             case when season = '一季度' then 
    ->                 orderCount 
    ->             end orderCount1, 
    ->             case when season = '二季度' then 
    ->                 orderCount 
    ->             end orderCount2, 
    ->             case when season = '三季度' then 
    ->                 orderCount 
    ->             end orderCount3, 
    ->             case when season = '四季度' then 
    ->                 orderCount 
    ->             end orderCount4 
    ->         from t_temp
    ->     ) t 
    -> group by year;
+---------+--------------+--------------+--------------+--------------+
| year    | 第一季度     | 第二季度     | 第三季度     | 第四季度     |
+---------+--------------+--------------+--------------+--------------+
| 2010年  |          100 |          200 |          300 |          400 |
| 2011年  |          150 |          300 |          450 |          600 |
+---------+--------------+--------------+--------------+--------------+
2 rows in set (0.00 sec)

4. 通过IF聚合函数实现

mysql> SELECT year, 
    ->        SUM(IF(season = '一季度', orderCount, null)) AS '第一季度',
    ->        SUM(IF(season = '二季度', orderCount, null)) AS '第二季度',
    ->        SUM(IF(season = '三季度', orderCount, null)) AS '第三季度',
    ->        SUM(IF(season = '四季度', orderCount, null)) AS '第四季度'
    -> FROM t_temp
    -> GROUP BY year;
+---------+--------------+--------------+--------------+--------------+
| year    | 第一季度     | 第二季度     | 第三季度     | 第四季度     |
+---------+--------------+--------------+--------------+--------------+
| 2010年  |          100 |          200 |          300 |          400 |
| 2011年  |          150 |          300 |          450 |          600 |
+---------+--------------+--------------+--------------+--------------+
2 rows in set (0.00 sec)

5. 总结

以需求驱动技术,技术本身没有优略之分,只有业务之分。

亿速云「云数据库 MySQL」免部署即开即用,比自行安装部署数据库高出1倍以上的性能,双节点冗余防止单节点故障,数据自动定期备份随时恢复。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×