Оценка стоимости запасов в порядке поступления (FIFO)

Вот интересная статья, которая показалась мне полезной для моего проекта:

Скорость Phreakery на основе множеств: проблема SQL инвентаризации FIFO:

Stock table which we use to track the track movements of stock in and out of our imaginary stock warehouse. Our warehouse is initially empty, and stock then moves into the warehouse as a result of a stock purchase (tranCode = 'IN'), or due to a subsequent return (tranCode = 'RET'), and stock moves out of the warehouse when it is sold (tranCode = 'OUT'). Each type of stock tem is indentified by an ArticleID. Each movement of stock in or out of the warehouse, due to a purchase, sale or return of a given item, results in a row being added to the Stock table, uniquely identified by the value in the StockID identity column, and describing how many items were added or removed, the price for purchases, the date of the transaction, and so on.

Хотя я использую это в своем текущем проекте, я застрял на том, как получить цену, взимаемую с каждой транзакции "OUT". Мне нужно иметь это значение, чтобы определить, сколько я буду взимать с моих клиентов.

First add 5 apples (each $10.00) to the stock, for a total of $50.00

Add 3 apples (each $20.00) to the stock total of 8 apples, for a total price of $110.00

Then take out 6 items (5 each $10.00 and 1 each $20.00) $70 total

After the transaction it will be leaving 2 apples @$20 each with a total of $40

 Here's my current table
 Item    transaction code    qty     price   
 apple   IN                    5     10.00    
 apple   IN                    3     20.00   
 apple   OUT                   6          

 Manual computation for the OUT transaction price (FIFO)
 QTY     price   total price 
 5       10.00   50.00 
 1       20.00   20.00 
 TOTAL:6         70.00 

 Output of the script:
 Item    CurrentItems   CurrentValue
 apple   2            40.00

 What I need:
 Item    transaction code    qty     price   CurrentItems    CurrentValue 
 apple   IN                    5     10.00   5               50.00 
 apple   IN                    3     20.00   8               110.00 
 apple   OUT                   6             2                   40.00 

 This too will be OK
 Item    transaction code    qty     price   CurrentItems    
 apple   IN                    5     10.00   0               
 apple   IN                    3     20.00   0                
 apple   OUT                   6         70 

Размещенный сценарий, который выиграл конкурс, был очень полезен, я надеюсь, что кто-нибудь может помочь мне узнать, как получить цену за «OUT». сделка

Ответы на вопрос(5)

Ваш ответ на вопрос