title: Which approach is the best to calculate a running total
body: <p>This evening I've been pondering about how to bet tackle a problem of a running total. I’m using C# with Entity Framework and SQL Server 2012 to develop an application that can analyze my bank transactions. </p>
  
  <p>In the database, I’ve created a table <code>Payment</code>, which contains the following properties: </p>
  
  <pre><code>PaymentDate, FromAccountId, ToAccountId, Amount
  </code></pre>
  
  <p>The latter property, <code>Amount</code>, is always positive; the direction of the transaction should be determined from the properties <code>FromAccountId</code> and <code>ToAccountId</code>. </p>
  
  <p>Now, I have to be able to calculate the running total at any given time. For instance, by subtracting the running total at the beginning of the month from that at the end of the month, I can deduce the balance for a specific account. Calculating this running total will play a significant role in the application, so a single calculation cannot take too long.</p>
  
  <p>I’ve come up with 4 strategies to calculate this running total:</p>
  
  <ol>
  <li>Use stored procedure where the time and the account are input parameters. </li>
  <li>Calculate the running total in my application (since I’m using Entity Framework, the calculation will de facto take place in the database)</li>
  <li>Creating a table <code>RunningTotal</code> and update it for every transaction. The downside to this approach is that I have to keeping this table up to date myself, i.e. more business logic.</li>
  <li>Similar to option 3, but then use a view that is based on the Payment table. This way, I should have to deal with extra business logic. </li>
  </ol>
  
  <p>The thing is, I cannot make up my mind which approach is the best. What are your thoughts?</p>