title: Calculate running difference between two columns in Postgresql
body: <p>I have data in two tables as below</p>
  <p>Table 1 :</p>
  <pre><code>Material |Ordr Qty 
  ---------|---------
  abcd     |4253
  </code></pre>
  <p>Table 2 :</p>
  <pre><code>Material | Stck Qty 
  ---------|---------
  abcd     |1000
  abcd     |2000
  abcd     |2000
  </code></pre>
  <p>Expected Output :</p>
  <pre><code>Material |Ordr Qty |Stck Qty |Column D
  ---------|---------|---------|---------
  abcd     |4253     |1000     |3253
  abcd     |4253     |2000     |1253
  abcd     |4253     |2000     |-747
  </code></pre>
  <p>Logic for column D is like</p>
  <pre><code>4253-1000 = 3253
  3253-2000 = 1253
  1253-2000 = -747
  </code></pre>
  <p><em>LAG(ordr qty - stck qty,1,0)over (ORDER BY material)-stck qty</em></p>
  <p>I am trying above LAG function and getting below output</p>
  <pre><code>abcd    4253    1000    -1000
  abcd    4253    2000    1253
  abcd    4253    2000    253
  </code></pre>
  <p>Let me know how to achieve the expected output.</p>