0

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

    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
)
);
4

1 回答 1

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 的参考文章:

11g 中的透视和反透视查询

于 2012-10-11T17:10:55.523 回答