mysql列轉行
測試資料:
mysql>create table sale(month int(6),product varchar(32),amount decimal(20,2)
mysql> insert into sale values(201502,'apple',3500.00);
Query OK, 1 row affected (0.01 sec)
mysql> insert into sale values(201502,'banana',3000.00);
Query OK, 1 row affected (0.01 sec)
mysql> insert into sale values(201502,'pear',1000.00);
Query OK, 1 row affected (0.01 sec)
mysql> insert into sale values(201502,'orange',1000.00);
Query OK, 1 row affected (0.01 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from sale;
+--------+---------+---------+
| month | product | amount |
+--------+---------+---------+
| 201502 | apple | 3500.00 |
| 201502 | banana | 3000.00 |
| 201502 | pear | 1000.00 |
| 201502 | orange | 1000.00 |
+--------+---------+---------+
4 rows in set (0.00 sec)
現在前端展現要求需要以product為行的資料,具體實現如下:
SELECT
month,
max(case when product='apple' then mount ELSE 0 END) apple,
max(case when product='banana' then mount ELSE 0 END) banana,
max(case when product='pear' then mount ELSE 0 END) pear,
max(case when product='orange' then mount ELSE 0 END) orange
FROM sale GROUP BY month;
+--------+---------+---------+---------+---------+
| month | apple | banana | pear | orange |
+--------+---------+---------+---------+---------+
| 201502 | 3500.00 | 3000.00 | 1000.00 | 1000.00 |
+--------+---------+---------+---------+---------+
1 row in set (0.00 sec)
注:GROUP BY之後一定要用max函式,不然會出現banana,pear,orange會出現空
原因:
SELECT
month,
case when product='apple' then mount ELSE 0 END apple,
case when product='banana' then mount ELSE 0 END banana,
case when product='pear' then mount ELSE 0 END pear,
case when product='orange' then mount ELSE 0 END orange
FROM sale 結果是:
+--------+---------+---------+---------+---------+
| month | apple | banana | pear | orange |
+--------+---------+---------+---------+---------+
| 201502 | 3500.00 | 0 | 0 | 0 |
| 201502 | 0 | 3000.00 | 0 | 0 |
| 201502 | 0 | 0 | 1000.00 | 0 |
| 201502 | 0 | 0 | 0 | 1000.00 |
+--------+---------+---------+---------+---------+
4 rows in set (0.00 sec)
group by後只會取第一條記錄,除了apple其他欄位都是0,所以要加個max,把每個欄位的最大值取出來。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29989552/viewspace-2085266/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Mysql - 行轉列、列轉行MySql
- mysql動態行轉列MySql
- mysql基礎 行轉列MySql
- MySQL 行轉列詳情鍾萼MySql
- 行轉列與列轉行
- SQL 行轉列,列轉行SQL
- hive經典案列--top N(行轉列\列轉行)Hive
- sql的行轉列(PIVOT)與列轉行(UNPIVOT)SQL
- oracle列轉行Oracle
- SQL列轉行SQL
- Oracle行轉列、列轉行的Sql語句總結OracleSQL
- mysql~GROUP_CONCAT實現關係表的行轉列MySql
- 【MySQL】MySQL如何合併多行資料,行轉列,group_concat 多行合併MySql
- My SQL 列轉行操作SQL
- oracle中多列轉行Oracle
- Hive行轉列函式Hive函式
- Databricks 第11篇:Spark SQL 查詢(行轉列、列轉行、Lateral View、排序)SparkSQLView排序
- mac中用命令列執行mysqlMac命令列MySql
- SQL 行轉列 PIVOT 學習示例SQL
- oracle行轉列、列轉行、連續日期數字實現方式及mybatis下實現方式OracleMyBatis
- excel列轉行怎麼做 excel如何轉置行列Excel
- mysql交換兩行或兩列的值MySql
- List,DataTable實現行轉列的通用方案
- 資料庫知識點(2)——行轉列資料庫
- Linux命令之grep/sed/awk等行轉列Linux
- iOS 字典轉陣列,陣列轉字典iOS陣列
- 列舉mysql正在執行中的全部事務MySql
- unnest array_contains 行轉列 id in效率高AI
- 直播平臺原始碼,FlinkSQL實現行轉列原始碼SQL
- Spark SQL中列轉行(UNPIVOT)的兩種方法SparkSQL
- 用listagg函式分組實現列轉行函式
- PHP xml 轉陣列 陣列轉 xml 操作PHPXML陣列
- MySQL主從複製錯誤——列型別轉換錯誤MySql型別
- js將陣列轉字元,字元轉回陣列JS陣列字元
- 在Linux命令列中進行大小寫字元轉換Linux命令列字元
- 數棧技術分享:解讀MySQL執行計劃的type列和extra列MySql
- MySQL虛擬列MySql
- MySQL 生成列索引MySql索引
- MySQL原理 - InnoDB引擎 - 行記錄儲存 - Off-page 列MySql