4 responses to “How to calculate a running balance using SQL Server !!

  1. Amir

    October 14, 2012 at 7:56 AM

    man your second query columns are not correct according to your tables

    but i have made correction to it

    SELECT [A].TranID,[c].CustID,[C].CustName,[A].TranDate,[A].DebitValue,[A].CreditValue,
    (SELECT SUM(COALESCE(DebitValue,0) – COALESCE(CreditValue,0))
    FROM dbo.AccountTransactions AS [B]
    WHERE [B].TranID <= [A].TranID AND [c].CustID = [c].CustID) AS 'Running Balance'
    FROM dbo.AccountTransactions AS [A]
    INNER JOIN dbo.Account AS [C] ON [c].CustID = [C].CustID
    ORDER BY CustID, TranID;

  2. cartoon characters at birthday party

    June 17, 2014 at 1:09 PM

    Hello, I check your new stuff regularly. Your story-telling style
    is awesome, keep it up!

    • Ahmed Negm

      June 17, 2014 at 1:31 PM

      Thanks a lot, you’re welcome!

  3. Raza

    January 29, 2016 at 1:45 PM

    there is a little problem with the script, it’s repeating the same result for 3 times. To work it correctly you should write the script like this..

    SELECT [A].TranID,
    (SELECT SUM(COALESCE(DebitValue,0) – COALESCE(CreditValue,0))
    FROM dbo.CustomersTransactions AS [B]
    WHERE [B].TranID <= [A].TranID) AS 'Running Balance'
    FROM dbo.CustomersTransactions AS [A]
    INNER JOIN dbo.Customers AS [C] ON [A].CustomerName = [C].CustID
    ORDER BY CustID, TranID


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: