开发者

How to do Pivoting in Oracle 10g

开发者 https://www.devze.com 2023-01-10 08:02 出处:网络
Consider the following Sample Input SalesBoyNameProductAmount ------------------------- Boy1P1100 Boy1P140

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

SELECT SalesBoyName, [P1] AS P1, [P2] AS P2,[P3] AS P3
FROM 
(SELECT * FRO开发者_Python百科M tblSales ) s
PIVOT
(
SUM (Amount)
FOR Product IN
( [P1], [P2], [P3])
) AS pvt    

I want to perform the same thing in Oracle 10g.

How to do this?

This may be trivial, but since i am very new to Oracle, so I am seeking for help.

Thanks


You can do it like this in 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消