---
title: How to get the running balance from a table with a single SQL Query
description: How to get the running balance from a table with a single SQL Query
---

[e-Zest members share technology ideas to foster digital transformation.](https://blog.e-zest.com)

# [How to get the running balance from a table with a single SQL Query](https://blog.e-zest.com/how-to-get-the-running-balance-from-a-table-with-a-single-sql-query/)

 Written by [Adel Merchant](https://blog.e-zest.com/author/adel-merchant) | Mar 7, 2008, 7:21:00 PM

 

**Table structure for Balance table-**

<https://blog.e-zest.com/hs-fs/file-3441638197-jpg/blog-files/image.axd_.jpg>

**Required result from the query -**

 

Like in a Bank's money transaction output, the **RunningBalance** column  
 is needed which displays the balance an account holds against that will account  
 transaction.

<https://blog.e-zest.com/hs-fs/file-3441638382-jpg/blog-files/image2.axd_.jpg>

**Query to achieve it -**

<https://blog.e-zest.com/hs-fs/file-3441638392-jpg/blog-files/image3.axd_.jpg>

**Explanation -**

The result is achieved by a self join of the **Balances** table. The first table b1 returns the complete table output, inner join has two conditions. The first condition '**b1.DateStamp \>= b2.DateStamp**' is the most important which gives the result for **SUM(b2.Amount)**. The last two records for BalanceID 4 and 6 have the exact same DateStamp hence without the second condition '**b1.BalanceID \>= b2.BalanceID**' will give same result for the RunningBalance column.

[View full post](https://blog.e-zest.com/how-to-get-the-running-balance-from-a-table-with-a-single-sql-query/)

```json
{
  "@context" : "http://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Adel Merchant"
  },
  "dateModified" : "2015-10-12T06:19:54.009Z",
  "datePublished" : "2008-03-07T19:21:00Z",
  "headline" : "How to get the running balance from a table with a single SQL Query",
  "image" : {
    "@type" : "ImageObject",
    "height" : 333,
    "url" : "http://cdn2.hubspot.net/hub/744339/file-3441638197-jpg/blog-files/image.axd_.jpg",
    "width" : 503
  },
  "mainEntityOfPage" : "https://blog.e-zest.com/how-to-get-the-running-balance-from-a-table-with-a-single-sql-query/",
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "height" : 60,
      "url" : "/hs/hsstatic/content_shared_assets/static-1.4092/img/default-amp-logo.png",
      "width" : 60
    },
    "name" : "Blogs"
  }
}
```