I want to create total field in this table. But in SQL server 2008 can't use 'order by' in OVER clause.
I have same PO and difference INV. I want to Qty field minus Quantity field and next row Total field minus Quantity field.
This is a structure table.
And this's a result.
This is my code but it's error at ORDER BY.
select t2.BaseRef 'PO',t2.BaseQty 'qty', t1.NumAtCard 'INV',
t2.Quantity 'Quantity',
t2.BaseQty - SUM(t2.Quantity) OVER (ORDER BY t1.DocDate) AS 'total'
from OPDN t1
INNER JOIN PDN1 t2 ON t1.DocEntry = t2.DocEntry
Can I use other way for sort this data?
Best Answer
If
SUM() OVER()
is not supported, you may try with the following approach. Rows are ordered by INV number as text (INV100, INV105, ...), not as number (100, 105, ...).Input:
Statement:
Output: