Oracle行列转换

pivot 列转行

select * 
from (select name, nums from demo)
pivot(sum(nums) for name
in ('苹果' 苹果, '橘子', '葡萄', '芒果'));

注意: pivot(聚合函数 for 列名 in(类型)) ,其中 in(‘’) 中可以指定别名,in中还可以指定子查询,比如 select distinct code from customers

unpivot 行转列

select id , name, jidu, xiaoshou 
from Fruit
unpivot (xiaoshou for jidu in (q1, q2, q3, q4));

注意: unpivot没有聚合函数,xiaoshou、jidu字段也是临时的变量

更多详细内容请点这里

Author: Monkey
Link: https://monkeyip.github.io/2018/12/08/Oracle行列转换/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.