title: MySQL Inventory / Stock Average Costing
body: <p>I have a table called "entries" where I save daily sale and purchase transactions / entries. "entries" table looks like:</p>
  
  <p>---Entries Table---</p>
  
  <pre><code>EntryDate    EntryType    Item    Qty    Rate    Cost    Sale
  2016-01-01   Purchase     Apples  10     10      100     -
  2016-01-02   Purchase     Apples  20     15      300     -
  2016-01-02   Purchase     Oranges 20     15      300     -
  2016-01-02   Sale         Apples  5      20      **      100
  ....
  ....
  </code></pre>
  
  <p>Now I need to run a query to <strong>update</strong> the [cost] column in above table where [EntryType] is 'Sale'. Cost will be calculated using Weighted Average Method i.e. Current Sale Row Cost = sum ( all previous costs of purchases - all previous [calculated]costs of sales ) / sum( all previous qty of purchases - all previous qty of sales ).</p>
  
  <p>I Need update query</p>
  
  <pre><code> update entires set ...
  </code></pre>