I'm getting an odd error, that I think I might know the answer to, but still can't seem to fix. It is saying that "LatestUpdatedDate" is an invalid identifier and I think it is because it runs the ORDER BY statement before it gets to the CASE statement, but that doesn't really make much sense with the way it is structured. Have any ideas?
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
The aliases will given after generating result set so you can't use them in Windowing functions. You can do this:
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
or wrap your subquery to another
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
PS I don't sure for oacle
syntax, but should you give aliases to subquery?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.