title: Linking together multiple database table entries from multiple months
body: <p>I am developing a personal finance tracker (for fun!) and I have a table of categories. Each category is an entry in the table and at the end of the month they are all duplicated with their relevant balances reset to the start of the month reading for the new month.</p>
  
  <p>Among others, these categories can be of type 'savings' and so have a running total. If I want to retrieve a category or update it then I used the category_id field and this works fine for the current working month but linking months together is breaking my brain. For the savings categories I want to show how the running_total has increased over the previous six months but in my current DB design, categories don't "know" about their previous months as they are created new at the start of each month. </p>
  
  <p>The only way I could currently retrieve the last 6 months of a savings running_total is to search by the category name but this is potentially unreliable.</p>
  
  <p>I have considered adding a field to the table which is "previous_month_category_id" which would work as a way to link the categories together but would be expensive to implement as it would require 6 MSQL operations each time grabbing the "previous_month_category_id" from the result and then re running the query.</p>
  
  <p>If MYSQL can do some kind of recursion then maybe this could work but I feel like there is a more obvious answer staring me in the face.</p>
  
  <p>I'm using Codeigniter and MYSQL but not scared of vanilla PHP if required.</p>
  
  <p>Help on how to do this would be great.</p>
  
  <p>UPDATE 1: </p>
  
  <p>Below is a sample from what the savings category might look like mixed in amongst other categories. At the end of each month the entry is duplicated with the same category_name, type, buget, year, and users_id but the category_id auto increments, the month updates to the new month number and the running total is the previous running_total + the budget. How would I do one database query to retrieve these without using the category_name? As this could change is the user decided to caller it "Bigger TV" at the end of July</p>
  
  <pre><code>+-------------+--------------+------+--------+---------------+------+-------+----------+
  | category_id |category_name | type | budget | running_total | year | month | users_id |
  +-------------+--------------+------+--------+---------------+------+-------+----------+
  |      44     | Big TV       | sav  |   20   |      240      | 2012 |   8   |   77     |
  
  +-------------+--------------+------+--------+---------------+------+-------+----------+
  |      32     | Big TV       | sav  |   20   |      220      | 2012 |   7   |   77     |
  
  +-------------+--------------+------+--------+---------------+------+-------+----------+
  |      24     | Big TV       | sav  |   20   |      200      | 2012 |   6   |   77     |
  </code></pre>
  
  <p>UPDATE 2:</p>
  
  <p>I'm not sure I'm explaining myself very well So I'll put some more detail around how the app works and see if that helps.</p>
  
  <p>I have tables called "categories", "transactions" and "users". A category can be one of three types, 1: Cash, 2: Regular Payment, 3: Savings. Think of cash and regular payment types as buckets, at the start of each month each bucket is full and the aim is to take money out of it and make sure there is still a bit left at the end of the month (or at least not negative).</p>
  
  <p>This is fine on a month by month basis and works very well (for me, I have used this system for 2 years now I think). The trip up comes with Savings as they are linked month by month and are more like a big bucket that is added to each month (with a set increment called budget) until it overspills and is then drained (like Big TV would be when you buy it), or taken from a little bit here and there and the aim is to build up an emergency fund (like "When my car breaks down" type thing).</p>
  
  <p>When the relevant information is displayed for each category only the current month is shown for cash and regular as that is all that is important, for the savings however the current amount is also shown but it would be nice to show a small history graph of how it had built up (or depleted) over time. To do this I need some way of searching for the previous end of month states of these categories so that the graph can be plotted but currently I can't work out how to link them all by anything other than the category_name.</p>
  
  <p>I have tried to implement a bit of DB normalisation but this is the first schema I've implemented having known about normalisation so I've probably missed some aspects of it and possibly avoided any over normalisation where it didn't feel right.</p>
  
  <p>Below are my tables:</p>
  
  <p>categories</p>
  
  <pre><code> +-------------+--------------+------+--------+---------------+------+-------+----------+
   | category_id |category_name | type | budget | running_total | year | month | users_id |
   +-------------+--------------+------+--------+---------------+------+-------+----------+
  </code></pre>
  
  <p>transactions</p>
  
  <pre><code>+----------------+--------------+--------+------+----------+------------------------+
  | transaction_id |  description | amount | date | users_id | categories_category_id |
  +----------------+--------------+--------+------+----------+------+-------+---------+
  </code></pre>
  
  <p>they are joined on categories_category_id which is a foreign key</p>
  
  <p>I have always worked off the premise that each category needs an new entry for each month but it seems from the comments and answers below that I would be better off having just one category entry regardless of month and then just calculating everything on the fly?</p>
  
  <p>Although, the budgets can be changed by the user and so for record keeping I'm not sure if this would work also the "deposits" never really happen it is just the category being duplicated at the end of the month so I guess that would need to dealt with.....</p>
  
  <p>The aim of this app has always been to decouple financial tracking from the physical transaction that occur in a bank account and provide a layer over someones finances thus allowing the user to avoid hard to explain transactions etc and just focus on over all cash position. There is no concept of an "income" in this system, or a bank account.</p>