考虑以下
示例输入
SalesBoyName Product Amount
------------ ------- ------
Boy1 P1 100
Boy1 P1 40
Boy1 P2 100
Boy2 P1 100
Boy2 P3 12
所需输出
SalesBoyName P1 P2 P3
------------ ---- ---- ----
Boy1 140 100 null
Boy2 100 null 12
以下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
我想在Oracle 10g中执行相同的操作。
怎么做?
这可能是微不足道的,但由于我对Oracle很新,所以我正在寻求帮助。
由于
答案 0 :(得分:5)
你可以在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;
在11G中有一个类似于SQL Server的PIVOT关键字。