优势数据库服务器 ORDER BY 行为

发布于 2024-08-29 22:25:05 字数 854 浏览 4 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(2

九八野马 2024-09-05 22:25:05

我认为您需要将 ORDER BY 移到子查询之外:

SELECT Name, Value, ROWNUM() FROM 
  (SELECT * FROM MainTable WHERE Value > 0 ) a ORDER BY Value

如果您希望将 rownum() 应用于有序结果集(今天早上我有点慢),那么可能需要使用类似以下:

SELECT Name, Value, ROWNUM() FROM 
  (SELECT top 100 PERCENT * FROM MainTable WHERE Value > 0 order by value ) a

我认为子查询中的 ORDER BY 是不允许的,除非它实际上改变了结果......但是在这种情况下,它似乎确实应该被允许。

I think you need to move the ORDER BY outside the subquery:

SELECT Name, Value, ROWNUM() FROM 
  (SELECT * FROM MainTable WHERE Value > 0 ) a ORDER BY Value

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:

SELECT Name, Value, ROWNUM() FROM 
  (SELECT top 100 PERCENT * FROM MainTable WHERE Value > 0 order by value ) a

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.

我的影子我的梦 2024-09-05 22:25:05

在语句中使用别名可以使其更清晰,并可能解决问题。

而不是:

SELECT Name, Value, ROWNUM() 
FROM (SELECT * FROM MainTable WHERE Value > 0 ORDER BY Value) a

使用

SELECT a.Name, a.Value, ROWNUM() 
FROM (SELECT mt.* FROM MainTable mt WHERE mt.Value > 0 ORDER BY mt.Value) a

问题可能是它混淆了 order by 子句中的字段 value ——它不知道从哪个表获取它,是第一个表还是第一个表。第二。

Use aliases in your statements to make it clearer, and probably to solve the issue.

Rather than:

SELECT Name, Value, ROWNUM() 
FROM (SELECT * FROM MainTable WHERE Value > 0 ORDER BY Value) a

Use

SELECT a.Name, a.Value, ROWNUM() 
FROM (SELECT mt.* FROM MainTable mt WHERE mt.Value > 0 ORDER BY mt.Value) a

The problem might be that it is confusing the field value in the order by clause -- it does not know which table to get it from, the 1st or 2nd.

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