这是一篇有趣的文章,我发现它对我的项目很有用:
Set-based Speed Phreakery: The FIFO Stock Inventory SQL Problem :
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”。我需要这个值来确定我将向客户收取多少费用。
首先向库存中添加 5 个苹果(每个 10.00 美元),总计 50.00 美元
在 8 个苹果的库存总数中添加 3 个苹果(每个 20.00 美元),总价为 110.00 美元
然后取出 6 件元素(5 件每件 10.00 美元,1 件每件 20.00 美元)总计 70 美元
交易完成后,将留下 2 个苹果,每个 20 美元,总计 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”交易的价格
最佳答案
我建议您设计如下表格: 在表中添加一个新字段,即 qty_out
出售前的表格:
Item transaction code qty qty_out price
apple IN 5 0 10.00
apple IN 3 0 20.00
apple OUT 6 null
出售 6 件商品后的表格:
Item transaction code qty qty_out price
apple IN 5 5 10.00
apple IN 3 1 20.00
apple OUT 6 null
您可以将“qty”与“qty_out”(对于 IN 交易)进行比较以找出价格。
关于sql - 先进先出 (FIFO) 库存成本核算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11204136/