6

我无法理解这段代码的错误。

代码:

SELECT
    CariID, HesapID, BTrh, BCinsiID, BNo, Acklm, VdTrh, mTrh, BorcT, AlacakT, 
    SUM(BorcT) OVER (PARTITION BY CariID, HesapID ORDER BY BTrh, BNo, mTrh) AS TopBorcT, 
    SUM(AlacakT) OVER (PARTITION BY CariID, HesapID ORDER BY BTrh, BNo, mTrh ) AS TopAlacakT
FROM
    tCariH

错误:

消息 102,第 15 级,状态 1,第 3 行

'order' 附近的语法不正确。

4

3 回答 3

2

With an aggregate function like SUM, you don't use ORDER BY in the OVER clause - you only need to use the PARTITION function. The ORDER is used for ranking functions:

Depending on the ranking, aggregate, or analytic function used with the OVER clause, <ORDER BY clause> and/or the <ROWS and RANGE clause> may not be supported.

Just modify to remove the ORDER in both your aggregates and you should be fine.

于 2012-04-05T12:40:52.517 回答
2

显然这在 SQL Server 2008 中不受支持,但在 SQL Server 2012 中支持

如何在 over 函数中使用 partition by 和 order by?

试试这个工具,你会明白为什么: http ://sqlfiddle.com/#!6/5303f/1

于 2013-04-24T18:23:10.190 回答
0
ID      AccountID       Quantity
1          1               10           Sum = 10
2          1               5                = 10 + 5 = 15
3          1               2                = 10 + 5 + 2 = 17
4          2               7                = 7
5          2               3                = 7 + 3 = 10  

SELECT ID, AccountID, Quantity, 
       SUM(Quantity) OVER (PARTITION BY AccountID ) AS TopBorcT, 
FROM tCariH
于 2012-04-06T05:22:43.517 回答