rownum 的 SQL 查询
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
因为同一查询的 WHERE 子句中不支持别名。因此,请像这样编写您的查询:
Because aliases are not supported in the WHERE clause of the same query. So instead write your query like:
首先,您不能使用
rownum = 2
条件引用第二行。您可以通过指定where rownum <<> 来选择前两行。 3
条件,或者您可以将其包装在另一个查询中,并将您的rownum
引用为那里的普通列。然后,您无法在分配了此别名的子查询的
where
子句中引用列别名。您可以将其提升一级:
或者只是避免此引用
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
您不能像这样在 where 子句中引用列别名。
rownum 也不会以这种方式停止工作。尝试这样的事情:
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:
@阿米特是对的。因为 Oracle 首先评估 WHERE 条件,然后评估 SELECT。您必须进行子选择。
而不是:
执行以下操作:
...r 现在对 WHERE 子句可见。
可能这个问题是相同/相似/重复的:
在 WHERE 中使用别名条款
@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
一种常见的获取方式是指获得前五名高薪员工。
A common way to get lets say the top five highly paid employees.