title: Creating a query to subtract different values from a single column into a new column
body: <p>Poorly worded title, but I can't think of a succinct way to describe my problem.</p>
  
  <p>I have a table with the following columns:</p>
  
  <pre><code>year | month | basin_id | value
  </code></pre>
  
  <p>I need to take the values for all basin_ids of one year/month and subtract from that the corresponding values for all basin_ids of another year/month, and store the resulting values in such a way that they are still associated with their respective basin_ids.</p>
  
  <p>This seems like it should be a rather simple query/subquery, and I can calculate the difference in values just fine with:</p>
  
  <pre><code>SELECT (val1.value-val2.value)
  FROM value_table_1 as val1, 
       value_table_2 as val2 
  WHERE val1.basin_id=val2.basin_id
  </code></pre>
  
  <p>where value_table_1 and value_table_2 are temporary tables I've made by segregating all values associated with year1/month1 and year2/month2 for the sake of simplifying my query.</p>
  
  <p>My problem from here is I get a column with all of the new values, but not with their associated basins. How can I achieve this? I am writing this within a plpgsql stored procedure, if that helps.</p>
  
  <p>Say my table is as follows:</p>
  
  <pre><code>year | month | basin_id | value
  -----+-------+----------+-------
  2017 |   04  |   123    |  10
  2017 |   04  |   456    |  6
  2017 |   05  |   123    |  12
  2017 |   05  |   456    |  4
  </code></pre>
  
  <p>and I'm given the inputs:</p>
  
  <pre><code>year1 := 2017
  month1 := 04
  year2 := 2017
  month2 := 05
  </code></pre>
  
  <p>I want to get the following table as a result:</p>
  
  <pre><code> basin_id | value
  ----------+------
     123    |  -2
     456    |  2
  </code></pre>