Sql query on rownum

2019-08-30 02:15发布

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

5条回答
Rolldiameter
2楼-- · 2019-08-30 02:43

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
         );
查看更多
我命由我不由天
3楼-- · 2019-08-30 02:47

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;
查看更多
forever°为你锁心
4楼-- · 2019-08-30 02:54

A common way to get lets say the top five highly paid employees.

SELECT ename,sal FROM   emp
WHERE  rownum <= 5
ORDER BY sal DESC;
查看更多
Deceive 欺骗
5楼-- · 2019-08-30 02:57

@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:

Using an Alias in a WHERE clause

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-08-30 03:06

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;
查看更多
登录 后发表回答