views:

51

answers:

4
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??

+1  A: 

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:

select instmax from
(
  SELECT instmax, row_number(instmax) order by (instmax desc nulls last) rownumber
  FROM pswlinstmax
)
where rownumber = 2;
jonearles
+2  A: 

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 a where rownum < 3 condition, or you may wrap it in another query and reference your rownum 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:

SELECT instmax
  FROM (SELECT instmax, rownum r
          FROM (SELECT instmax
                  FROM pswlinstmax
                 ORDER BY instmax DESC NULLS LAST)
         )
         WHERE r = 2;

or just avoid this reference

-- this will return first two rows
SELECT instmax
  FROM (SELECT instmax, rownum r
          FROM (SELECT instmax
                  FROM pswlinstmax
                 ORDER BY instmax DESC NULLS LAST)

         WHERE rownum < 3
         );
be here now
+3  A: 

Because aliases are not supported in the WHERE clause of the same query. So instead write your query like:

SELECT instmax
FROM 
  (SELECT instmax ,rownum r 
  FROM 
    ( SELECT instmax FROM pswlinstmax ORDER BY instmax DESC NULLS LAST 
    )  
) WHERE r = 2;
Amit
A: 

@Amit is right. Because Oracle first evaluates the WHERE condition, then SELECT. You have to do sub-select.

instead of:

SELECT instmax
FROM(
  SELECT instmax ,rownum r
  FROM (SELECT instmax FROM pswlinstmax ORDER BY instmax DESC NULLS LAST)
  WHERE r = 2
  );

do the following:

SELECT instmax
FROM ( SELECT instmax ,rownum r
       FROM ( SELECT instmax FROM pswlinstmax ORDER BY instmax DESC NULLS LAST )
)
WHERE r = 2
;

...r is now visible to the WHERE clause. Propably this question is identicial/similar/duplicate for this:

http://stackoverflow.com/questions/356675/using-an-alias-in-a-where-clause

Martin Mares