rownum 的 SQL 查询

发布于 2024-09-16 15:44:49 字数 365 浏览 6 评论 0原文

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

执行后出现此错误:

ORA-00904: "R": invalid identifier
00904. 00000 -  "%s: invalid identifier"
*Cause:    
*Action:
Error at Line: 39 Column: 8

为什么会出现此错误?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

安静被遗忘 2024-09-23 15:44:49

因为同一查询的 WHERE 子句中不支持别名。因此,请像这样编写您的查询:

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

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;
不可一世的女人 2024-09-23 15:44:49

首先,您不能使用 rownum = 2 条件引用第二行。您可以通过指定 where rownum <<> 来选择前两行。 3 条件,或者您可以将其包装在另一个查询中,并将您的 rownum 引用为那里的普通列。

然后,您无法在分配了此别名的子查询的 where 子句中引用列别名。
您可以将其提升一级:

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

或者只是避免此引用

-- 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
         );

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
         );
任谁 2024-09-23 15:44:49

您不能像这样在 where 子句中引用列别名。

rownum 也不会以这种方式停止工作。尝试这样的事情:

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

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;
诗化ㄋ丶相逢 2024-09-23 15:44:49

@阿米特是对的。因为 Oracle 首先评估 WHERE 条件,然后评估 SELECT。您必须进行子选择。

而不是:

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

执行以下操作:

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

...r 现在对 WHERE 子句可见。
可能这个问题是相同/相似/重复的:

在 WHERE 中使用别名条款

@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

绮筵 2024-09-23 15:44:49

一种常见的获取方式是指获得前五名高薪员工。

SELECT ename,sal FROM   emp
WHERE  rownum <= 5
ORDER BY sal DESC;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文