How to use OVER and ORDER BY

I can not understand this code error.

CODE:

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

Error:

Msg 102, Level 15, State 1, Line 3

Invalid syntax near order.

+5
source share
3 answers

With an aggregate function, such as SUM, you do not use ORDER BYin a sentenceOVER , you only need to use the function PARTITION. ORDERused to rank functions:

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

Just change to remove ORDERin both of your aggregates and everything will be fine.

+2
source

-, SQL Server 2008, SQL Server 2012

?

, , : http://sqlfiddle.com/#!6/5303f/1

+1
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
0
source

All Articles