我有以下查询,我添加了一个
ROW_NUMBER PARTITION BY
窗口函数,并将其包装在外部 SELECT 语句中。我收到 ORA-30483: window functions are not allowed here
错误。
(SELECT * FROM
(SELECT A.INVENTORY_ITEM_ID
,C.SUBINVENTORY_CODE,
TO_CHAR(MAX(B.COST_DATE)) COST_DATE,
to_char(B.TRANSACTION_COST,'FM99990.90') TRANSACTION_COST
,ROW_NUMBER() OVER (PARTITION BY C.SUBINVENTORY_CODE ORDER BY A.INVENTORY_ITEM_ID, B.COST_DATE DESC) rownum1
FROM CST_ONHAND_V A,
CST_ITEM_COST_HISTORY_V B,
CST_TXN_LAYER_DTLS_V C
WHERE 1=1
AND A.INVENTORY_ITEM_ID= :p_inv_num
AND A.REC_TRXN_ID=B.TRANSACTION_ID
AND A.COST_ORG_ID = B.COST_ORG_ID
AND A.COST_BOOK_ID = B.COST_BOOK_ID
AND A.INVENTORY_ITEM_ID=B.INVENTORY_ITEM_ID
AND A.REC_TRXN_ID=C.REC_TRXN_ID
GROUP BY A.INVENTORY_ITEM_ID
,C.SUBINVENTORY_CODE
,to_char(B.TRANSACTION_COST,'FM99990.90')
,ROW_NUMBER() OVER (PARTITION BY C.SUBINVENTORY_CODE ORDER BY A.INVENTORY_ITEM_ID, B.COST_DATE DESC )
ORDER BY COST_DATE DESC NULLS LAST
)
WHERE rownum1 = 1 AND TRANSACTION_COST IS NOT NULL )
根据您的评论,INVENTORY_ITEM_ID 和 SUBINVENTORY_CODE 需要最长日期 (COST_DATE),
INVENTORY_ITEM_ID 可以包含在partition by 子句中,并可以从group_by 子句中删除ROW_NUMBER。
SELECT * FROM (
SELECT
A.INVENTORY_ITEM_ID,
C.SUBINVENTORY_CODE,
TO_CHAR(MAX(B.COST_DATE)) COST_DATE,
TO_CHAR(MAX(B.TRANSACTION_COST), 'FM99990.90') TRANSACTION_COST,
ROW_NUMBER() OVER (PARTITION BY A.INVENTORY_ITEM_ID, C.SUBINVENTORY_CODE ORDER BY B.COST_DATE DESC) AS rownum1
FROM
CST_ONHAND_V A
INNER JOIN CST_ITEM_COST_HISTORY_V B ON A.REC_TRXN_ID = B.TRANSACTION_ID
AND A.COST_ORG_ID = B.COST_ORG_ID
AND A.COST_BOOK_ID = B.COST_BOOK_ID
AND A.INVENTORY_ITEM_ID = B.INVENTORY_ITEM_ID
INNER JOIN CST_TXN_LAYER_DTLS_V C ON A.REC_TRXN_ID = C.REC_TRXN_ID
WHERE A.INVENTORY_ITEM_ID = :p_inv_num
GROUP BY A.INVENTORY_ITEM_ID, C.SUBINVENTORY_CODE
ORDER BY COST_DATE DESC NULLS LAST
)
WHERE rownum1 = 1 AND TRANSACTION_COST IS NOT NULL;