title: FIFO based stock inventory valuation in SQL Server
body: <p>I have a stock transaction table like this:</p>
  
  <pre><code>Item   Date         TxnType Qty  Price
  ABC   01-April-2012   IN    200 750.00
  ABC   05-April-2012   OUT   100     
  ABC   10-April-2012   IN     50 700.00
  ABC   16-April-2012   IN     75 800.00
  ABC   25-April-2012   OUT   175     
  XYZ   02-April-2012   IN    150 350.00
  XYZ   08-April-2012   OUT   120     
  XYZ   12-April-2012   OUT    10     
  XYZ   24-April-2012   IN     90 340.00
  </code></pre>
  
  <p>I need the value of the inventory for each item in FIFO (First in first out) meaning the first purchased item should be consumed first.
  The output stock valuation of the above data is:</p>
  
  <pre><code>Item  Qty      Value
  ABC   50    40000.00
  XYZ   110   37600.00
  </code></pre>
  
  <p>Please help me to get the solution.</p>