如何在 Oracle 10g 中进行透视
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3386803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do Pivoting in Oracle 10g
提问by Tony Andrews
Consider the following
考虑以下
Sample Input
样本输入
SalesBoyName Product Amount
------------ ------- ------
Boy1 P1 100
Boy1 P1 40
Boy1 P2 100
Boy2 P1 100
Boy2 P3 12
Desired Output
期望输出
SalesBoyName P1 P2 P3
------------ ---- ---- ----
Boy1 140 100 null
Boy2 100 null 12
The below SQL SERVER 2005 query will do the work
下面的 SQL SERVER 2005 查询将完成这项工作
SELECT SalesBoyName, [P1] AS P1, [P2] AS P2,[P3] AS P3
FROM
(SELECT * FROM tblSales ) s
PIVOT
(
SUM (Amount)
FOR Product IN
( [P1], [P2], [P3])
) AS pvt
I want to perform the same thing in Oracle 10g.
我想在 Oracle 10g 中执行相同的操作。
How to do this?
这该怎么做?
This may be trivial, but since i am very new to Oracle, so I am seeking for help.
这可能是微不足道的,但由于我对 Oracle 非常陌生,所以我正在寻求帮助。
Thanks
谢谢
回答by Tony Andrews
You can do it like this in 10G:
你可以在 10G 中这样做:
select salesboyname,
sum (case when product='P1' then amount end) as p1,
sum (case when product='P2' then amount end) as p2,
sum (case when product='P3' then amount end) as p3
from tblsales
group by salesboyname;
In 11G there is a PIVOT keyword similar to SQL Server's.
在 11G 中有一个类似于 SQL Server 的 PIVOT 关键字。