SELECT instmax
FROM
(SELECT instmax ,rownum r
FROM
( SELECT instmax FROM pswlinstmax ORDER BY instmax DESC NULLS LAST
)
WHERE r = 2
);
After execution it's giving this error:
ORA-00904: "R": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 39 Column: 8
why it's giving this error??
First of all, you can't reference to a second row using a
rownum = 2
condition. You can either select first two rows by specifying awhere rownum < 3
condition, or you may wrap it in another query and reference yourrownum
as an ordinary column from over there.Then, you can't reference a column alias in a
where
clause of a subquery this alias was assigned. You can either bring it one level up:or just avoid this reference
Because aliases are not supported in the WHERE clause of the same query. So instead write your query like:
A common way to get lets say the top five highly paid employees.
@Amit is right. Because Oracle first evaluates the WHERE condition, then SELECT. You have to do sub-select.
instead of:
do the following:
...r is now visible to the WHERE clause. Propably this question is identicial/similar/duplicate for this:
Using an Alias in a WHERE clause
You can't reference a column alias in the where clause like that.
The rownum won't quit work that way either. Try something like this: