将Access PIVOT查询转换为Oracle 11g

时间:2012-10-11 16:03:07

标签: oracle oracle11g pivot-table crosstab

我有一个MS Access PIVOT查询,需要迁移到Oracle 11g。

    TRANSFORM Format(Sum(tblOrderLine.ItemQtyCur), '#0.0') AS [The Value]

SELECT  tblOrderLine.OrderStatus, tblOrderLine.OrdNo, tblOrderLine.OrdlNo, Format(Sum(tblOrderLine.ItemQtyCur), '#0.0')  As SumConfQty

FROM tblOrder INNER JOIN (tblOrderLine LEFT JOIN tblArticle ON tblOrderLine.ItemId = tblArticle.ArtNo) ON tblOrder.OrdNo = tblOrderLine.OrdNo

GROUP BY  tblOrderLine.OrderStatus, tblOrderLine.OrdNo, tblOrderLine.OrdlNo

PIVOT tblOrderLine.LastDate

我无法将其转换为oracle 11g PIVOT。

任何人都可以帮助我....

编辑:

它会喜欢这个......

WITH pivot_data AS (
SELECT  tblOrderLine.OrderStatus, tblOrderLine.OrdNo, tblOrderLine.OrdlNo, Format(tblOrderLine.ItemQtyCur, '#0.0')  As SumConfQty
FROM tblOrder INNER JOIN (tblOrderLine LEFT JOIN tblArticle ON tblOrderLine.ItemId = tblArticle.ArtNo) ON tblOrder.OrdNo = tblOrderLine.OrdNo
GROUP BY  tblOrderLine.OrderStatus, tblOrderLine.OrdNo, tblOrderLine.OrdlNo
)
SELECT * FROM pivot_data
PIVOT XML (
Format(Sum(tblOrderLine.ItemQtyCur), '#0.0')
FOR tblOrderLine.LastDate
IN (
SELECT  tblOrderLine.LastDate
FROM tblOrder INNER JOIN (tblOrderLine LEFT JOIN tblArticle ON tblOrderLine.ItemId = tblArticle.ArtNo) ON tblOrder.OrdNo = tblOrderLine.OrdNo
GROUP BY  tblOrderLine.OrderStatus, tblOrderLine.OrdNo, tblOrderLine.OrdlNo
)
);

1 个答案:

答案 0 :(得分:1)

您的PIVOT语法应该是这样的:

select *
from
(
    SELECT tblOrderLine.OrderStatus, 
        tblOrderLine.OrdNo, 
        tblOrderLine.OrdlNo, 
        tblOrderLine.ItemQtyCur,
        tblOrderLine.LastDate
    FROM tblOrder 
    INNER JOIN tblOrderLine 
        ON tblOrder.OrdNo = tblOrderLine.OrdNo
    LEFT JOIN tblArticle 
        ON tblOrderLine.ItemId = tblArticle.ArtNo 
) x
pivot
(
    sum(ItemQtyCur)
    for LastDate in (yourDatesHere)
) p

如果您想使用PIVOT XML,则语法与此类似:

select *
from
(
    SELECT tblOrderLine.OrderStatus, 
        tblOrderLine.OrdNo, 
        tblOrderLine.OrdlNo, 
        tblOrderLine.ItemQtyCur,
        tblOrderLine.LastDate
    FROM tblOrder 
    INNER JOIN tblOrderLine 
        ON tblOrder.OrdNo = tblOrderLine.OrdNo
    LEFT JOIN tblArticle 
        ON tblOrderLine.ItemId = tblArticle.ArtNo 
) x
pivot XML
(
    sum(ItemQtyCur) as SumItemQtyCur
    for (LastDate) in (SELECT DISTINCT LastDate FROM tblOrderLine)
) p

以下是有关Oracle Pivots的参考文章:

pivot and unpivot queries in 11g