我正在尝试使用滞后函数,这样我就可以在不使用光标的情况下将一列与最后一列进行比较。然而,我需要比较的列必须使用别名,因为我使用了3个联合)。下面是我的一个例子。
SELECT
'Y' AS paid,
lag(paid,1) over (ORDER BY salary) AS prev_paid
FROM pay
UNION
SELECT
'N' as paid,
lag(paid,1) over (ORDER BY salary) AS prev_paid
FROM not_paid
我一直收到错误:PL/SQL:ORA-00904:"已支付":无效标识符
我怀疑你想要更像这样的东西:
SELECT paid, lag(paid,1) over (ORDER BY salary) AS prev_paid
FROM
(
SELECT 'Y' as paid, salary
FROM pay
UNION
SELECT 'N' as paid, salary
FROM not_paid
)
一般答案是否定的:在Oracle中,除非在order by
子句中,否则永远不能在定义列的级别使用列别名。
但是,您的查询还有其他问题,因为您得到的是常量的lag
值@Tony Andrew的询问似乎是你真正想要的。