SQL Server - How to solve FIFO Cost of OHB... function, cursor,? SQL Server 2008R2 - Need to solve FIFO cost of items on hand. 
To do this I need to add the cost for the most recent receipts quantity up to the current on hand balance(OHB). 

So lets say I have 
    Item : b1234 
    OHB  : 5

Receipts:
RctNumb | Item  | QTYRct | RctCostPerItem | DATERECD
rct05   | b1234 |      1 |           1.00 | 20160822
rct04   | b1234 |      3 |           2.00 | 20160820
rct03   | b1234 |      3 |           1.25 | 20160819
rct02   | b1234 |      2 |           2.50 | 20160818
rct01   | b1234 |      8 |           2.00 | 20160810


Here I would need:
(1*1.00)+(3*2.00)+(1*1.25) = CostofOHB = 8.25 

I have no idea how to address this with SQL.
Kathi Kellenberger did a comparison of FIFO costing for Redgate in 2010. I used the winning entry method to calculate monthly inventory quantities and values for our Mexico operations. You can search for Set based speed PHreakery if this link does not work. https://www.red-gate.com/simple-talk/sql/performance/set-based-speed-phreakery-the-fifo-stock-inventory-sql-problem/