title: Looping through a database using multiple tables (and returning a SUM)
body: <p>I currently have a database with a table of transactions that an apartment owner makes in a set of apartments</p>
  <p>When I run this query:</p>
  <pre><code>SELECT Account, Sum([Debit]) - SUM([Credit]) as Balance
    FROM [Boschendal Manor BC].[dbo].[PostGL] as PostGl 
    Inner JOIN [Boschendal Manor BC].[dbo].[Client] as Client on Client.DCLink = DrCrAccount
    where DrCrAccount = '2' and AccountLink &lt;&gt; '104' and TxDate &lt; '2021/11/30' Group By Account
  </code></pre>
  <p>It shows the Account number and the Balance before the 30th Of November for unit 1 - and if I increase DrCrAccount to 3, it will show the same for unit 2 ... and so on</p>
  <p>So I need to loop through the number of clients in the database and get each account number and balance.</p>
  <p>I have figured out how to get the number of clients, like so:</p>
  <pre><code>select COUNT(*) from [Boschendal Manor BC].[dbo].[Client] where DCBalance is not NULL
  </code></pre>
  <p>This returns <code>130</code></p>
  <p>Does anyone know how I can loop through the PostGL table for as long as the Clients table is , and then return the sum of debit and credit where the date is before 2021/11/30?</p>