2011-10-08 236 views
2

我試圖讓運行在我看來,總計在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筆交易爲同一天,它不能正確地計算它..任何想法?

回答

6

執行當前行計算,一個CTE因爲你在2008年

WITH transactionTotal AS 
(
    SELECT t.Created, t.Description, t.Credit, t.Debit, t.TransDate, t.TransactionID, a.AccountID 
     , ROW_NUMBER() OVER (ORDER BY TransDate ASC) AS RowNumber 
     , (ISNULL(t.Credit, 0) - ISNULL(t.Debit, 0)) AS TransactionTotal 
    FROM dbo.Transactions AS t 
    INNER JOIN dbo.BankAccounts AS a ON t.AccountID = a.AccountID 
) 
SELECT t.Created, t.Description, t.Credit, t.Debit, t.TransDate, t.TransactionID, t.AccountID 
    , (SELECT SUM(tt.TransactionTotal) 
     FROM transactionTotal AS tt 
     WHERE tt.RowNumber <= t.RowNumber) AS RunningTotal 
FROM transactionTotal AS t 
LEFT JOIN transactionTotal AS tt ON t.RowNumber = tt.RowNumber + 1 
ORDER BY t.TransDate DESC 
+0

非常好!一直試圖得到這個好幾天,只是不得不處理錯誤的總數,直到現在...把它放到一個視圖中,它工作的很棒 – jaekie

1
SELECT t.Created, t.Description, t.Credit, t.Debit, t.TransDate, t.TransactionID, ba.AccountID, 
     coalesce((select sum(ISNULL(Credit,0) - ISNULL(Debit, 0)) 
       from Transactions 
       where TransactionID <= t.TransactionID and 
         AccountID = ba.AccountID and 
         convert(date, TransDate) = convert(date, t.TransDate)),0) 
     AS [Running Total] 

FROM Transactions t INNER JOIN 
     dbo.BankAccounts ba ON t.AccountID = ba.AccountID 
+0

似乎不工作,並失蹤(合併後...我會編輯帖子,以顯示我得到的 – jaekie

+0

我相信這個問題可能會在你如何得到從日期的小日期,我會更新我的答案 –

+1

好的,我更新了,看到另一個變化是我將'Running Total'替換爲[Running Total],在我的測試中'Running Total'命名它不起作用 –

0

如果這是Oracle,那麼就有Window函數。你可以使用鉛和/或LAG相對於我以前ROW_NUMBER之前或即將行(根據排序順序)

+0

其SQL Server,我會更新問題 – jaekie

1

- 我會使用現有的標識列100%確定我正在處理正確的事務。

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.TransactionID < t.TransactionID 
           and b.AccountID = t.AccountID),0) 
           AS RunningTotal 
FROM Transactions t 
INNER JOIN dbo.BankAccounts ba ON t.AccountID = ba.AccountID 

- 兼如果你改變了「低於」,以「小於或等於」,那麼你就不必添加當前項目:

SELECT t.Created, t.Description, t.Credit, t.Debit, t.TransDate, t.TransactionID, ba.AccountID, 
COALESCE((SELECT SUM(isnull(Credit,0)) - SUM(isnull(Debit,0)) 
           FROM Transactions b 
           WHERE b.TransactionID <= t.TransactionID 
           and b.AccountID = t.AccountID),0) 
           AS RunningTotal 
FROM Transactions t 
INNER JOIN dbo.BankAccounts ba ON t.AccountID = ba.AccountID 

總計應該是:(假設)起始餘額:49.25

TransDate    Credit   Debit   RunningTotal  
----------------------- ----------------- -------------- ----------------- 
2011-10-08 20:14:00  NULL    12    37.25 
2011-10-08 20:14:00  2.11    NULL   39.36    
2011-10-07 20:14:00  42.25    NULL   81.61 
2011-10-06 20:14:00  NULL    12.25   69.36 
2011-10-05 20:14:00  31.25    NULL   100.61