使用具有“order by”组件的窗口函数时,结果集排序是什么?

发布于 2025-01-03 19:23:14 字数 368 浏览 2 评论 0原文

我正在 SEDE 上进行查询:

select top 20 
  row_number() over(order by "percentage approved" desc, approved desc), 
  row_number() over(order by "total edits" asc), 
  *
from editors 
where "total edits" > 30

考虑到两个窗口函数,结果集的顺序是什么?

我怀疑它未定义,但找不到明确的答案。 OTOH,使用此类窗口函数的查询结果是根据 over(order by ...) 子句排序的。

I'm working on a query on the SEDE:

select top 20 
  row_number() over(order by "percentage approved" desc, approved desc), 
  row_number() over(order by "total edits" asc), 
  *
from editors 
where "total edits" > 30

What is the ordering of the result set, taking into account the two window functions?

I suspect it's undefined but couldn't find a definitive answer. OTOH, results from queries with one such window function were ordered according to the over(order by ...) clause.

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

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

发布评论

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

评论(2

早茶月光 2025-01-10 19:23:14

结果可以按任何顺序返回。

现在,它们通常会按照 OVER 子句中指定的顺序返回,但这只是因为 SQL Server 可能会选择一个对行进行排序的查询计划来计算聚合。这绝不是保证的,因为它可以随时选择不同的查询计划,特别是当您使查询变得更加复杂时,这会扩展可能的查询计划的空间。

The results can be returned in any order.

Now, they will often be returned in the same order as specified in the OVER clause, but this is just because SQL Server is likely to pick a query plan that sorts the rows to calculate the aggregate. This is by no means guaranteed, as it could pick a different query plan at any time, especially as you make your query more complex which extends the space of possible query plans.

梦里人 2025-01-10 19:23:14

没有显式 ORDER BYANY SQL Server 查询的结果集是未定义的。

这包括当您在查询中使用窗口函数或在子查询中使用 ORDER BY 时。结果顺序将取决于很多因素,除非您指定 ORDER BY,否则无法保证这些因素。

The result set of ANY SQL Server query that doesn't have an explicit ORDER BY is undefined.

This includes when you have window functions within the query, or an ORDER BY in a subquery. The result order will depend on a lot of factors, none of which are guaranteed unless you specify an ORDER BY.

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