【发布时间】:2014-07-19 07:22:32
【问题描述】:
我在 Oracle 表 (tab1) 中有以下示例数据,我正在尝试将行转换为列。我知道如何在一列上使用 Oracle Pivot。但是可以将它应用于多个列吗?
样本数据:
Type weight height
A 50 10
A 60 12
B 40 8
C 30 15
我的预期输出:
A-count B-count C-count A-weight B-weight C-weight A-height B-height C-height
2 1 1 110 40 30 22 8 15
我能做什么:
with T AS
(select type, weight from tab1 )
select * from T
PIVOT (
count(type)
for type in (A, B, C, D,E,F)
)
上面的查询给了我下面的结果
A B C
2 1 1
我可以用sum(weight) 或sum(height) 替换count(*) 来调整高度或重量。我想做但做不到的是,在一个查询中以所有三个(计数、体重和身高)为中心。
可以用pivot来完成吗?
【问题讨论】:
-
非常好的问题,谢谢!
标签: sql oracle oracle11g pivot