SQL 服务器 - 包含 ROWS BETWEEN UNBOUNDED PRECEDING 的分区不起作用?
SQL Server - Over partition with ROWS BETWEEN UNBOUNDED PRECEDING not working?
根据 documentation:
UNBOUNDED PRECEDING Applies to: SQL Server 2012 (11.x) through SQL
Server 2017.
Specifies that the window starts at the first row of the partition.
UNBOUNDED PRECEDING can only be specified as window starting point.
那怎么会,用这个的时候:
avg(Qty) over (partition by [Name]
ORDER BY [Period] desc ROWS BETWEEN UNBOUNDED PRECEDING AND 2 FOLLOWING)
我仍然得到滚动平均值。
我明白上面的构造应该让我相当于 Select TOP(3) avg(qty)...
,至少根据文档。
我是不是漏掉了什么?
在研究、测试和分析可用文档之后,要记住的一件事是我的理解是不完整的:在任何配置中使用 ROWS
时,参考点始终是当前行。所以我的查询:
avg(Qty) over (partition by [Name]
ORDER BY [Period] desc ROWS BETWEEN UNBOUNDED PRECEDING AND 2 FOLLOWING)
就是"get all rows from start of the partition of the window to current row and 2 rows more"的意思。未说出口的部分是"current row",无论何时都在那里。
感谢 Damien_the_Unbeliever 陈述显而易见的事情,这让我一直在思考。
根据 documentation:
UNBOUNDED PRECEDING Applies to: SQL Server 2012 (11.x) through SQL Server 2017.
Specifies that the window starts at the first row of the partition. UNBOUNDED PRECEDING can only be specified as window starting point.
那怎么会,用这个的时候:
avg(Qty) over (partition by [Name]
ORDER BY [Period] desc ROWS BETWEEN UNBOUNDED PRECEDING AND 2 FOLLOWING)
我仍然得到滚动平均值。
我明白上面的构造应该让我相当于 Select TOP(3) avg(qty)...
,至少根据文档。
我是不是漏掉了什么?
在研究、测试和分析可用文档之后,要记住的一件事是我的理解是不完整的:在任何配置中使用 ROWS
时,参考点始终是当前行。所以我的查询:
avg(Qty) over (partition by [Name]
ORDER BY [Period] desc ROWS BETWEEN UNBOUNDED PRECEDING AND 2 FOLLOWING)
就是"get all rows from start of the partition of the window to current row and 2 rows more"的意思。未说出口的部分是"current row",无论何时都在那里。
感谢 Damien_the_Unbeliever 陈述显而易见的事情,这让我一直在思考。