title: Sum values from 2 joined tables even if id's don't match
body: <p>I'm trying to take two tables, join them together based on their contract numbers and then get a sum of each customers contract amount between two years.</p>
  <p>However, some of these contracts were from an older system in which we do not have record of them in the current system. What we do have is an archived table that shows what the price of the contract was, but no ties to the old contract.  The reason I can't just take it from the archived table is that even after these contracts get archived, they can be deleted or cancelled anytime after that.</p>
  <p>So, I'm trying to figure out how I can join both of these tables and get the sum of the customers contract totals when some are tied to a contract number and some are not?</p>
  <p>I know this is hard to explain but hopefully by showing the schema and query it will help.</p>
  <p>This is our archives table schema which holds an archive of every single contract a customer has had from both the old and new system.</p>
  <pre><code>|customer_id | contract_amount | date       | contract_number |
  |1           | 300.00          | 2019-02-01 | abc123          |
  |1           | 250.00          | 2019-02-03 | abc456          |
  |1           | 320.00          | 2019-04-02 | abc789          |
  |1           | 230.00          | 2019-07-03 | efg123          |
  </code></pre>
  <p>Contract table schema:</p>
  <pre><code>|customer_id | contract_amount | date_authorized | contract_number | status   |
  |1           | 300.00          | 2019-01-03      | abc123          | Approved |
  |1           | 250.00          | 2019-01-06      | abc456          | Cancelled|
  |1           | 320.00          | 2019-02-01      | abc789          | Approved |
  </code></pre>
  <p>Here is the query I'm currently running to get customer 1's contract total between 2020 and 2019:</p>
  <pre><code>select a.customer_id
       , sum(a.contract_amount) as year_total 
    from Archives_Table a 
    left 
    join Contracts_Table c 
      on (a.contract_number = c.contract_number) 
   where a.date &gt;= '2019-01-01' 
     and a.date &lt; '2020-01-01' 
     and c.status = 'Approved' 
     and a.customer_id = '1';
  </code></pre>
  <p>And the result I get is:</p>
  <pre><code>|customer_id | year_total |
  |1           | 620.00     |
  </code></pre>
  <p>But I should be getting this:</p>
  <pre><code>|customer_id | year_total |
  |1           | 850.00     |
  </code></pre>
  <p>The contract number efg123 comes from the old system which we do not have record of the actual contract data in the new system so it should be added to the <code>year_total</code>.  I tried a normal join, left join, and right join to see if that would change the data but no luck.  I tried looking online for questions like this but couldn't find anything for this kind of situation.  If anyone could point me in the right direction or knows how I can solve this issue, that would be great.</p>