從表開始;計算SQL中的平均每日餘額
[AccountLedger]
AccountCode PostingDate Amount
128 2014-01-01 08:36:09 200.00
128 2014-01-01 14:18:10 200.00
128 2014-01-01 14:26:56 0.00
128 2014-01-01 18:17:31 400.00
128 2014-01-01 20:18:53 100.00
128 2014-01-02 00:10:35 0.00
128 2014-01-02 01:44:26 300.00
128 2014-01-02 15:49:31 -300.00
128 2014-01-03 00:33:23 400.00
128 2014-01-03 11:55:13 -200.00
128 2014-01-03 11:56:34 -100.00
128 2014-01-03 14:58:42 -400.00
128 2014-01-03 17:31:11 0.00
**REQUIRED RESULT**
AccountCode PostingDate daily_balance
128 2014-01-01 900.00
128 2014-01-02 900.00
128 2014-01-03 600.00
查詢被附加
select
Acc
, Dte
, sum(daily_amt) over (PARTITION BY Acc ORDER BY Dte DESC) as daily_balance
from (select
[AccountLedger].[AccountCode] as Acc
, convert(date, [AccountLedger].[PostingDate]) as Dte
, sum([AccountLedger].[Amount]) as daily_amt
from [AccountLedger]
WHERE [AccountLedger].[PostingDate] < '2014-04-01'
and [AccountLedger].[AccountCode]=128
group by [AccountLedger].AccountCode
, [AccountLedger].[PostingDate]
) t
order by Acc, dte*
*但出現錯誤;
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near 'order'.
Msg 102, Level 15, State 1, Line 16
Incorrect syntax near 't'.*
我該如何得到需要的結果?
什麼是爲了通過ACC,DTE *?什麼是dte *?在這種情況下,我相信錯誤消息說明了數量......並且相當準確。 – JonH
這篇文章沒有顯示研究工作。 – dfundako
Dte正在發佈日期 在此處的文本編輯器中格式化時,*符號會被嚴重附加。 問題來自over子句;它工作正常,直到「PARTITION BY Acc」和錯誤出現「ORDER BY DTE DESC」 – 3439027