我試圖讓運行在我看來,總計在SQL Server 2008運行總計
這裏是我的表
BankAccounts ------------ AccountID (KEY) Name Created Transactions ------------ TransactionID (KEY) Description Credit Debit TransDate Created AccountID
這裏是我的查詢到目前爲止..
SELECT t.Created, t.Description, t.Credit, t.Debit, t.TransDate, t.TransactionID, ba.AccountID, (isnull(t.Credit,0)-isnull(t.Debit,0))+COALESCE((SELECT SUM(isnull(Credit,0)) - SUM(isnull(Debit,0)) FROM Transactions b WHERE b.TransDate < t.TransDate and b.AccountID = t.AccountID),0) AS RunningTotal FROM Transactions t INNER JOIN dbo.BankAccounts ba ON t.AccountID = ba.AccountID
什麼,我得到的是..
TransDate Credit Debit RunningTotal ----------------------- ---------------------- ---------------------- --------------------- 2011-10-08 20:14:00 NULL 12 49.25 2011-10-08 20:14:00 2.11 NULL 63.36 2011-10-07 20:14:00 42.25 NULL 61.25 2011-10-06 20:14:00 NULL 12.25 19 2011-10-05 20:14:00 31.25 NULL 31.25
它應該是什麼樣子......
TransDate Credit Debit Running Total ----------------------- ---------------------- ---------------------- --------------------- 2011-10-08 00:31:32.957 NULL 12 51.36 2011-10-08 00:31:32.957 2.11 NULL 63.36 2011-10-07 00:31:32.957 42.25 NULL 61.25 2011-10-06 00:31:32.957 NULL 12.25 19 2011-10-05 00:31:32.960 31.25 NULL 31.25
我真的接近..似乎只是當有2筆交易爲同一天,它不能正確地計算它..任何想法?
非常好!一直試圖得到這個好幾天,只是不得不處理錯誤的總數,直到現在...把它放到一個視圖中,它工作的很棒 – jaekie