优势数据库服务器 ORDER BY 行为
我正在使用 ADS v10 测试版。我正在尝试计算有序结果集。
1) 嵌套查询中的 ORDER BY。我需要使用嵌套 SELECT 进行一些计算:
SELECT Name, Value, ROWNUM() FROM (SELECT * FROM MainTable WHERE Value > 0 ORDER BY Value) a
我发现
Expected lexical element not found: ) There was a problem parsing the table names after the FROM keyword in your SELECT statement.
当删除 ORDER BY 时, 一切都运行良好。虽然我在帮助中找到了示例,但它看起来像我的查询(确实更复杂):
SELECT * FROM (SELECT TOP 10 empid, fullname FROM branch1 ORDER BY empid) a UNION SELECT empid, fullname FROM branch2 ORDER BY empid
2) ORDER BY
+ ROWNUM()
。我在上面的示例中使用了嵌套查询来计算有序行。有没有机会避免嵌套查询? 在 SQL Server 中我可以做这样的事情:
SELECT Name, Value, ROW_NUMBER() OVER(ORDER BY Value) FROM MainTable WHERE Value > 1 ORDER BY Value
请指教。 谢谢。
I'm using ADS v10 beta. I'm trying to numerate ordered resultset.
1) ORDER BY in nested queries. I need to use nested SELECT for some calculations:
SELECT Name, Value, ROWNUM() FROM (SELECT * FROM MainTable WHERE Value > 0 ORDER BY Value) a
And I'm getting
Expected lexical element not found: ) There was a problem parsing the table names after the FROM keyword in your SELECT statement.
Everything is working well when the ORDER BY is removed. Although, I found the sample in the Help, it looks like my query (more complex, indeed):
SELECT * FROM (SELECT TOP 10 empid, fullname FROM branch1 ORDER BY empid) a UNION SELECT empid, fullname FROM branch2 ORDER BY empid
2) ORDER BY
+ ROWNUM()
. I used the nested query in the example above, to numerate ordered rows. Is there are any chance to avoid nested query?
In the SQL Server I can do something like this:
SELECT Name, Value, ROW_NUMBER() OVER(ORDER BY Value) FROM MainTable WHERE Value > 1 ORDER BY Value
Please advice.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您需要将 ORDER BY 移到子查询之外:
如果您希望将 rownum() 应用于有序结果集(今天早上我有点慢),那么可能需要使用类似以下:
我认为子查询中的 ORDER BY 是不允许的,除非它实际上改变了结果......但是在这种情况下,它似乎确实应该被允许。
I think you need to move the ORDER BY outside the subquery:
If you are wanting the rownum() to be applied to the ordered result set (I'm a bit slow this morning), then it might be necessary to use something like the following:
I don't think the ORDER BY in the subquery is allowed unless it actually changes the result ... however in this case, it does seem like it should be allowed.
在语句中使用别名可以使其更清晰,并可能解决问题。
而不是:
使用
问题可能是它混淆了
order by
子句中的字段value
——它不知道从哪个表获取它,是第一个表还是第一个表。第二。Use aliases in your statements to make it clearer, and probably to solve the issue.
Rather than:
Use
The problem might be that it is confusing the field
value
in theorder by
clause -- it does not know which table to get it from, the 1st or 2nd.