sql - 先进先出 (FIFO) 库存成本核算

标签 sql sql-server-2008

这是一篇有趣的文章,我发现它对我的项目很有用:

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”。我需要这个值来确定我将向客户收取多少费用。

  1. 首先向库存中添加 5 个苹果(每个 10.00 美元),总计 50.00 美元

  2. 在 8 个苹果的库存总数中添加 3 个苹果(每个 20.00 美元),总价为 110.00 美元

  3. 然后取出 6 件元素(5 件每件 10.00 美元,1 件每件 20.00 美元)总计 70 美元

  4. 交易完成后,将留下 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/

相关文章:

java - 处理超出需要的属性

sql - 在 SQL Server 中连接 2 个数字

sql-server - 另一个多对多关系问题

sql-server - 为什么此查询会生成 PK 违规错误?

java - JOOQ:更新空值的列值

php - 输出数据库列中的记录数并将其分配为变量

sql - Impala 分析函数在 where 子句中

java - JPA调用存储过程从2 db中选择数据

使用 LEN 对 ntext 字段进行 SQL SUM

SQL Server : How to perform Rtrim on all varchar columns of a table