SQL Server错误:'BETWEEN ... FOLLOWING AND CURRENT ROW'不是有效的窗口框架,不能与OVER子句一起使用。'?

时间:2017-04-21 10:59:15

标签: sql-server sql-server-2014

我试图将3个月的净值与一个月的净值相加。代码

SELECT [CMONTH]
      ,[ID]
      ,[NET_VALUE_1M]
      ,SUM(CAST([NET_VALUE_1M] as float)) OVER 
          (PARTITION BY [ID] ORDER BY [CMONTH] 
             ROWS BETWEEN 3 FOLLOWING AND CURRENT ROW) as 'NET_VALUE_3M'   
  FROM [Channel_AGG]

正在触发错误

Msg 4193, Level 16, State 4, Line 2
'BETWEEN ... FOLLOWING AND CURRENT ROW' is not a valid window frame 
and cannot be used with the OVER clause.

为什么,造成这种情况的原因以及如何解决?

1 个答案:

答案 0 :(得分:3)

您需要使用

ROWS BETWEEN CURRENT ROW AND 3 FOLLOWING

The upper bound cannot be smaller than the lower bound.