SQL Server 2008 R2 中的窗口函数问题

SQL Server 2008 R2 中的窗口函数问题

问题描述:

我在以下 SQL 脚本中遇到执行错误:

I get an execution error in following SQL script:

SELECT TOP 1 PERCENT
    a.accode, a.voucherdate, a.credit, a.Debit,
    SUM(a.Debit) OVER (ORDER BY [a.accode],[a.voucherdate]) AS rdr 
FROM
    VoucherMain AS a 
ORDER BY 
    a.accode, a.voucherdate

错误信息

'order' 附近的语法不正确

Incorrect syntax near 'order'

谁能告诉我我的语法有什么问题?

Can anyone tell me what's wrong with my syntax?

问题是你需要 SQL Server 2012 及更高版本.好的,我为未来的访问者添加了及以上",但比较 2008 OVER CLAUSE 带有 2012 年结束条款.

The problem is that you need SQL Server 2012 and above. Okay, I added the "and above" for future visitors, but compare 2008 OVER CLAUSE with 2012 OVER CLAUSE.

2008 版有以下重要说明:

The 2008 version has this important note:

在排名窗口函数的上下文中使用时, 只能引用由 FROM 子句提供的列.不能指定整数来表示名称的位置或选择列表中列的别名. 不能与聚合窗口函数一起使用.

When used in the context of a ranking window function, <ORDER BY Clause> can only refer to columns made available by the FROM clause. An integer cannot be specified to represent the position of the name or alias of a column in the select list. <ORDER BY Clause> cannot be used with aggregate window functions.