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 に答える