我得到一个奇怪的错误,我想我可能知道答案,但似乎仍然无法修复。它说“LatestUpdatedDate”是一个无效的标识符,我认为这是因为它在到达CASE语句之前运行ORDER BY语句,但这对于它的结构方式并没有多大意义。有什么想法吗?
SELECT * FROM
(
SELECT sym.*,
(
CASE WHEN shr_last_updt_dt >= trd_last_updt_dt
THEN shr_last_updt_dt
ELSE trd_last_updt_dt END
) AS LatestUpdatedDate,
row_number() over ( ORDER BY LatestUpdatedDate DESC ) AS line_number
FROM trdg_sym sym
WHERE ((wkly_rpt_dt = :settleDate)) AND ((lower(rpt_type_cd)=lower(:reportType)))
)
WHERE line_number BETWEEN 1 AND 25 ORDER BY line_number
答案 0 :(得分:3)
生成结果集后会给出别名,因此您无法在窗口函数中使用它们。你可以这样做:
SELECT * FROM
(
SELECT sym.*,
(
CASE WHEN shr_last_updt_dt >= trd_last_updt_dt
THEN shr_last_updt_dt
ELSE trd_last_updt_dt END
) AS LatestUpdatedDate,
row_number() over ( ORDER BY
(
CASE WHEN shr_last_updt_dt >= trd_last_updt_dt
THEN shr_last_updt_dt
ELSE trd_last_updt_dt END
) DESC ) AS line_number
FROM trdg_sym sym
WHERE ((wkly_rpt_dt = :settleDate)) AND ((lower(rpt_type_cd)=lower(:reportType)))
)
WHERE line_number BETWEEN 1 AND 25 ORDER BY line_number
或将子查询包装到另一个
SELECT * FROM
(
SELECT *,
row_number() over ( ORDER BY LatestUpdatedDate DESC ) AS line_number
FROM
(
SELECT sym.*,
(
CASE WHEN shr_last_updt_dt >= trd_last_updt_dt
THEN shr_last_updt_dt
ELSE trd_last_updt_dt END
) AS LatestUpdatedDate
FROM trdg_sym sym
WHERE ((wkly_rpt_dt = :settleDate)) AND ((lower(rpt_type_cd)=lower(:reportType)))
)
)
WHERE line_number BETWEEN 1 AND 25 ORDER BY line_number
P.S。我不确定oacle
语法,但是你应该给子查询提供别名吗?