【发布时间】:2014-12-26 21:55:11
【问题描述】:
我使用的是 sql server 2008。
我将 envetory 计算为 FIFO 方法,就像在计数中一样。
我有多个表输出的视图,所以创建示例表
create table #StockView
(
StoreId int,
ProductId int,
TranDate Date,
TransectionType varchar(5),
Quantity numeric(18,3),
UnitCost numeric(18,2)
)
insert into #StockView select 1 , 512 , '2013-03-30 ' , 'IN' , 1870, 35.57
insert into #StockView select 1 , 512 , '2013-04-05 ' , 'OUT' , 500 , 0
insert into #StockView select 1 , 512 , '2013-04-05 ' , 'OUT' , 150 , 0
insert into #StockView select 1 , 512 , '2013-04-15 ' , 'OUT' , 15 , 0
insert into #StockView select 1 , 512 , '2013-04-23 ' , 'OUT' , 125 , 0
insert into #StockView select 1 , 512 , '2013-04-25 ' , 'OUT' , 40 , 0
insert into #StockView select 1 , 512 , '2013-04-26 ' , 'OUT' , 734 , 0
insert into #StockView select 1 , 512 , '2013-05-15 ' , 'IN' , 1520, 35
insert into #StockView select 1 , 512 , '2013-05-15 ' , 'IN' , 1520, 35
insert into #StockView select 1 , 512 , '2013-05-23 ' , 'OUT' , 40 , 0
insert into #StockView select 1 , 512 , '2013-06-05 ' , 'OUT' , 70 , 0
insert into #StockView select 1 , 512 , '2013-06-06 ' , 'OUT' , 50 , 0
insert into #StockView select 1 , 512 , '2013-06-07 ' , 'OUT' , 286 , 0
insert into #StockView select 1 , 512 , '2013-06-10 ' , 'OUT' , 50 , 0
insert into #StockView select 1 , 512 , '2013-06-11 ' , 'OUT' , 41 , 0
insert into #StockView select 1 , 512 , '2013-06-15 ' , 'OUT' , 150 , 0
insert into #StockView select 1 , 512 , '2013-06-17 ' , 'OUT' , 700 , 0
insert into #StockView select 1 , 512 , '2013-06-21 ' , 'OUT' , 433 , 0
insert into #StockView select 1 , 512 , '2013-06-25 ' , 'OUT' , 20 , 0
insert into #StockView select 1 , 512 , '2013-06-26 ' , 'OUT' , 25 , 0
select * from #StockView
StoreId ProductId TranDate TransectionType Quantity UnitCost
1 512 2013-03-30 IN 1870.000 35.57
1 512 2013-04-05 OUT 500.000 0.00
1 512 2013-04-05 OUT 150.000 0.00
1 512 2013-04-15 OUT 15.000 0.00
1 512 2013-04-23 OUT 125.000 0.00
1 512 2013-04-25 OUT 40.000 0.00
1 512 2013-04-26 OUT 734.000 0.00
1 512 2013-05-15 IN 1520.000 35.00
1 512 2013-05-15 IN 1520.000 35.00
1 512 2013-05-23 OUT 40.000 0.00
1 512 2013-06-05 OUT 70.000 0.00
1 512 2013-06-06 OUT 50.000 0.00
1 512 2013-06-07 OUT 286.000 0.00
1 512 2013-06-10 OUT 50.000 0.00
1 512 2013-06-11 OUT 41.000 0.00
1 512 2013-06-15 OUT 150.000 0.00
1 512 2013-06-17 OUT 700.000 0.00
1 512 2013-06-21 OUT 433.000 0.00
1 512 2013-06-25 OUT 20.000 0.00
1 512 2013-06-26 OUT 25.000 0.00
请通过 https://www.simple-talk.com/sql/performance/set-based-speed-phreakery-the-fifo-stock-inventory-sql-problem/ 或者 calculate closing Stock Quantity,price & value by FIFO
上面的链接给出了可用库存或期末库存及其价值,但我正在寻找接受商店代码、产品 ID、fromDate 和结束日期的商店程序或函数,并且预期输出基于上述数据是
opening : 数据应该在日期之前,即给定 1,例如 @fromDate
请帮助我,因为我正在尝试获得这份报告,因为超过 6 个月
现在我几乎完成了,只是我在外向率方面遇到了问题。如果我根据 FIFO 获得外向费率,那么我的问题就会解决。 我已经编写了返回开放数量、费率和价值的商店程序。 通过此链接https://www.simple-talk.com/sql/performance/set-based-speed-phreakery-the-fifo-stock-inventory-sql-problem/。 除了outwardRate,其他都很容易计算。
inwardQty = sum(Quantity) where transectionType = 'IN' and TransDate 在@startDate 和@EndDate 之间
inwardRate = sum(inwardQty * UnitCost)/ Sum(inwardQty)
inwardValue = sum(inwardQty) * (sum(inwardQty * UnitCost)/ Sum(inwardQty) )
outwardQty = sum(Quantity) where transectionType = 'Out' and TransDate 在@startDate 和@EndDate 之间
outWardRate = ?? -- 这里我遇到了问题,如何根据 FIFO 方法获取外向速率
OutwardValue=outwardQty *outWardRate
ClosingQty = (opningQty + inwardQty)-outwardQty
ClosingRate = ((OpningValue + InwardValue) - OutwardValue) / ((opningQty + inwardQty ) - outsideQty)
ClosingValue = (OpningValue + InwardValue) - OutwardValue
【问题讨论】:
-
你是如何对
Opning进行分类的,因为表中没有这样的条目,为什么2014-04-01 to 2014-04-30向内为零,它有IN条目 -
感谢您的回复,在 4 月 3 月关闭数据到来之前 4 月 opning。如果在 4 月份检查数据没有任何内部产品,这就是它为零的原因。我有编辑图像,我计算了 3 月和 4 月的数据,请检查一下。如果您有任何疑问请询问。并使用我的名字@naweez 开始您的评论,以便我得到暗示。
标签: sql sql-server