SQL:Last_Value()返回错误的结果(但First_Value()工作正常)

Ech*_*cho 11 sql sql-server sql-function sql-server-2012

我在SQL Server 2012中有一个表,如快照所示:

在此输入图像描述

然后我使用Last_Value()和First Value来获取不同YearMonth的每个EmpID的AverageAmount.脚本如下:

SELECT A.EmpID,  
       First_Value(A.AverageAmount) OVER (PARTITION BY A.EmpID Order by A.DimYearMonthKey asc) AS  '200901AvgAmount', 
       Last_Value(A.AverageAmount) OVER (PARTITION BY A.EmpID Order by A.DimYearMonthKey asc) AS '201112AvgAmount'

FROM  Emp_Amt  AS A
Run Code Online (Sandbox Code Playgroud)

但是,此查询的结果是:

结果

在"201112AvgAmount"列中,它显示每个EmpID的不同值,而"200901AvgAmount"具有正确的值.

我的SQL脚本有什么问题吗?我在网上做了很多研究,但仍然找不到答案....

Luk*_*der 18

这是一个快速查询来说明行为:

select 
  v,

  -- FIRST_VALUE() and LAST_VALUE()
  first_value(v) over(order by v) f1,
  first_value(v) over(order by v rows between unbounded preceding and current row) f2,
  first_value(v) over(order by v rows between unbounded preceding and unbounded following) f3,
  last_value (v) over(order by v) l1,
  last_value (v) over(order by v rows between unbounded preceding and current row) l2,
  last_value (v) over(order by v rows between unbounded preceding and unbounded following) l3,

  -- For completeness' sake, let's also compare the above with MAX()
  max        (v) over() m1,
  max        (v) over(order by v) m2,
  max        (v) over(order by v rows between unbounded preceding and current row) m3,
  max        (v) over(order by v rows between unbounded preceding and unbounded following) m4
from (values(1),(2),(3),(4)) t(v)
Run Code Online (Sandbox Code Playgroud)

可以在这里看到上述查询的输出(这里SQLFiddle):

| V | F1 | F2 | F3 | L1 | L2 | L3 | M1 | M2 | M3 | M4 |
|---|----|----|----|----|----|----|----|----|----|----|
| 1 |  1 |  1 |  1 |  1 |  1 |  4 |  4 |  1 |  1 |  4 |
| 2 |  1 |  1 |  1 |  2 |  2 |  4 |  4 |  2 |  2 |  4 |
| 3 |  1 |  1 |  1 |  3 |  3 |  4 |  4 |  3 |  3 |  4 |
| 4 |  1 |  1 |  1 |  4 |  4 |  4 |  4 |  4 |  4 |  4 |
Run Code Online (Sandbox Code Playgroud)

很少有人会想到应用于带有ORDER BY子句的窗口函数的隐式框架.在这种情况下,窗口默认为框架RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.(RANGE与ROWS并不完全相同,但这是另一个故事).以这种方式思考:

  • v = 1有序窗口的框架跨度的行上v IN (1)
  • v = 2有序窗口的框架跨度的行上v IN (1, 2)
  • v = 3有序窗口的框架跨度的行上v IN (1, 2, 3)
  • v = 4有序窗口的框架跨度的行上v IN (1, 2, 3, 4)

如果要阻止该行为,您有两种选择:

  • ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING有序窗口函数使用显式子句
  • ORDER BY在那些允许省略它们的窗口函数中使用no 子句(as MAX(v) OVER())

更多细节解释的这篇文章LEAD(),LAG(),FIRST_VALUE()LAST_VALUE()


www*_*www 12

您的脚本没有任何问题,这是SQL Server中分区的工作方式:/.如果将LAST_VALUE更改为MAX,结果将相同.解决方案是:

SELECT A.EmpID,  
       First_Value(A.AverageAmount) OVER (PARTITION BY A.EmpID Order by A.DimYearMonthKey asc) AS  '200901AvgAmount', 
       Last_Value(A.AverageAmount) OVER (PARTITION BY A.EmpID Order by A.DimYearMonthKey ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS '201112AvgAmount'  
FROM  Emp_Amt  AS A
Run Code Online (Sandbox Code Playgroud)

关于它有一个很棒的帖子,链接.GL!