[英]How to do Pivoting in Oracle 10g
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 谢谢
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中,有一个PIVOT关键字类似于SQL Server。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.