如何在 Oracle 10g 中进行透视

发布于 2024-09-12 02:40:30 字数 873 浏览 2 评论 0原文

考虑以下

示例输入

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 很陌生,所以我正在寻求帮助。

谢谢

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 * FROM 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鼻尖触碰 2024-09-19 02:40:30

在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关键字。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文