title: Subtract values in each row of a column based on a WHERE and GROUP BY statement in SQL
body: <p>I would like to subtract each row &quot;Value&quot; with the &quot;Value&quot; where Sub1=0 grouping by ID_1 and ID_2 using a SQL query.</p>
  <p>This is the table structure:</p>
  <pre><code>------------------------------------
  ID_1 |ID_2 | sub1 | Value
  ------------------------------------
  1    | a   | 0    | 20  
  1    | a   | 50   | 30   
  1    | a   | 100  | 40   
  1    | b   | 0    | 25   
  1    | b   | 50   | 30   
  1    | b   | 100  | 50   
  2    | a   | 0    | 5  
  2    | a   | 50   | 10   
  2    | a   | 100  | 30   
  2    | b   | 0    | 25   
  2    | b   | 50   | 50   
  2    | b   | 100  | 70   
  </code></pre>
  <p>I would like to group by ID_1 and ID_2 and subtract each row's value with the value where the Sub1=0</p>
  <p>Output table should be :</p>
  <pre><code>------------------------------------
  ID_1 |ID_2 | sub1 | Value | Diff
  ------------------------------------
  1    | a   | 0    | 20    | 0
  1    | a   | 50   | 30    | 10
  1    | a   | 100  | 40    | 20
  1    | b   | 0    | 25    | 0
  1    | b   | 50   | 30    | 5
  1    | b   | 100  | 50    | 25
  2    | a   | 0    | 5     | 0
  2    | a   | 50   | 10    | 5
  2    | a   | 100  | 30    | 25
  2    | b   | 0    | 25    | 0
  2    | b   | 50   | 50    | 25
  2    | b   | 100  | 70    | 45
  </code></pre>