i have a table with a list of items and qty to be sold by a certain due date. I am trying to calculate a running total column that would show how many items would have been sold at a certain. below is my attempt to get the running totals but it doesn't work as I expect.
select item, due_date, qty, sum(qty) over (Order by item )
from apsplan
Where item ='19-3102875'
order by item
I get the error:
Msg 102, Level 15, State 1, Line 44 Incorrect syntax near 'order'.
ps I am using SQL Server 2012.
Here is the alternative suggestion I received from here
SELECT
item,
due_date,
qty,
(SELECT SUM(t2.qty) FROM apsplan t2 WHERE t2.item <= t1.item and item = '196-31020-005') AS rolling_qty
FROM apsplan t1
WHERE
item = '196-31020-005'
ORDER BY
item
here is the result - not the running total but the over
item due_date qty rolling_qty
196-31020-005 2017-09-20 00:59:00.000 1.00000000 24.00000000
196-31020-005 2017-10-06 01:00:00.000 1.00000000 24.00000000
196-31020-005 2017-11-06 01:00:00.000 1.00000000 24.00000000
196-31020-005 2017-12-06 01:00:00.000 1.00000000 24.00000000
196-31020-005 2019-01-28 01:00:00.000 1.00000000 24.00000000
196-31020-005 2019-02-04 01:00:00.000 1.00000000 24.00000000
196-31020-005 2019-03-25 01:00:00.000 1.00000000 24.00000000
196-31020-005 2019-03-25 01:00:00.000 1.00000000 24.00000000
196-31020-005 2019-04-01 01:00:00.000 1.00000000 24.00000000
196-31020-005 2019-04-08 01:00:00.000 1.00000000 24.00000000
196-31020-005 2019-04-29 01:00:00.000 1.00000000 24.00000000
196-31020-005 2019-05-06 01:00:00.000 1.00000000 24.00000000
196-31020-005 2019-05-13 01:00:00.000 1.00000000 24.00000000
196-31020-005 2019-05-27 01:00:00.000 1.00000000 24.00000000
196-31020-005 2019-06-03 01:00:00.000 1.00000000 24.00000000
196-31020-005 2019-06-10 01:00:00.000 1.00000000 24.00000000
196-31020-005 2019-07-01 01:00:00.000 1.00000000 24.00000000
196-31020-005 2019-07-08 01:00:00.000 1.00000000 24.00000000
196-31020-005 2019-07-15 01:00:00.000 1.00000000 24.00000000
196-31020-005 2019-07-29 01:00:00.000 1.00000000 24.00000000
196-31020-005 2019-08-05 01:00:00.000 1.00000000 24.00000000
196-31020-005 2019-08-12 01:00:00.000 1.00000000 24.00000000
196-31020-005 2019-08-26 01:00:00.000 1.00000000 24.00000000
196-31020-005 2019-09-02 01:00:00.000 1.00000000 24.00000000
This is too long for a comment.
Even though you are using SQL Server 2012, the actual functionality depends on the compatibility level. You can query for the compatibility level using:
SELECT name, compatibility_level FROM sys.databases
This should be at least 110 for you to use the order by
with sum()
. To change the compatibility, refer to the documentation . If you have an older compatibility (perhaps less than 100 is needed), then you can get this error.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.