ykwll 发表于 2018-9-29 09:59:42

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

mysql> select year, sum(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)


页: [1]
查看完整版本: MySQL DML操作--------实现pivot行转列功能最佳实战