首页 > 解决方案 > Oracle:将列转置为行

问题描述

我有下表中的数据

  bins  |   pro
  -------------
    1   |   0.10
    2   |   0.05
    3   |   0.78
    4   |   0.20 
    5   |   0.82
    6   |   0.45

我需要一个带有此结果的查询:

   1    |   2   |   3   |   4   |   5   |   6
-------------------------------------------------
  0.10  | 0.05  |  0.78 |  0.20 |  0.82 |  0.45

标签: sqloraclepivotaggregate-functions

解决方案


对于固定的 bin 列表,您可以进行条件聚合:

select 
    max(case when bins = 1 then pro end) as bins1,
    max(case when bins = 2 then pro end) as bins2
    ...
    max(case when bins = 6 then pro end) as bins6
from mytable

推荐阅读