0

I'm studying T-SQL set operators and Window functions. I'm following a video lecture on Microsoft Virtual Academy but I'm encountering a syntax error I can't solve.

I have a dummy Transactions table:

CREATE TABLE [dbo].[Transactions]
(
    [TransactionID] [int] IDENTITY(1,1) NOT NULL,
    [AccountID] [int] NOT NULL,
    [Amount] [money] NOT NULL,
    [TransactionDate] [datetime] NOT NULL
)

that I filled with some random data:

INSERT [dbo].[Transactions] ([AccountID], [Amount], [TransactionDate]) 
VALUES (1, 20.5000, '2018-07-03'), (1, 19.9900, '2018-07-03'),
       (2, 65.3000, '2018-07-03'), (2, 99.0000, '2018-07-04'),
       (1, 13.3000, '2018-07-04'), (1, 12.9900, '2018-07-04'),
       (2, 37.2500, '2018-07-04'), (1, 50.0000, '2018-07-05'),
       (1, 17.3500, '2018-07-05'), (2, 24.9000, '2018-07-05'),
       (1, 11.2500, '2018-07-05'), (2, 39.9000, '2018-07-05')

What I want now (following the lecture) is showing all the columns plus an additional column CurrentBalance, showing the balance after the amount is added to the previous amounts, like this:

TransactionID AccountID   Amount                TransactionDate         Balance
------------- ----------- --------------------- ----------------------- -----------
1             1           20,50                 2018-07-03 00:00:00.000 20,50
2             1           19,99                 2018-07-03 00:00:00.000 40,49
5             1           13,30                 2018-07-04 00:00:00.000 53,79
6             1           12,99                 2018-07-04 00:00:00.000 66,78
.
.
.

According to the lecture and the documentation on MSDN, the following query should do

SELECT
    *,
    SUM(Amount) OVER(PARTITION BY AccountID ORDER BY TransactionID, AccountID) AS Balance
FROM 
    Transactions
ORDER BY 
    TransactionID, AccountID

but I'm shown this error message:

Messaggio 102, livello 15, stato 1, riga 6
Incorrect syntax near 'order'.

As for the comments stating this is not the code I get the error from, here is a screenshot

enter image description here

I'm using SSMS v17.6 on a remote SQL Server 2008 R2.

Thanks, Davide.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Davide Vitali
  • 1,017
  • 8
  • 24

0 Answers0