库存表:
StoreNo Date ProductBarCode ProductQty
61 2016-02-28 2017961746012 100
61 2016-02-29 2017961746012 100
61 2016-03-01 2017961746012 100
61 2016-03-02 2017961746012 100
61 2016-03-03 2017960624045 100
61 2016-03-04 2017961746012 100
75 2016-03-04 2017960624045 90
61 2016-03-05 2017961746012 100
销售表:
StoreNo Date ProductBarCode SaleQty
61 2016-02-29 2017961746012 8
75 2016-03-04 2017960624045 0
61 2016-03-01 2017961746012 2
61 2016-03-04 2017961746012 -5
所需输出:
StoreNo Date ProductBarCode ProductQty
61 2016-02-28 2017961746012 100
61 2016-02-29 2017961746012 92
61 2016-03-01 2017961746012 90
61 2016-03-02 2017961746012 90
61 2016-03-03 2017960624045 100
61 2016-03-04 2017961746012 95
75 2016-03-04 2017960624045 90
61 2016-03-05 2017961746012 93
我想通过计算带有时间戳的(库存+所有附加产品进来)-所有已售产品
来确定实际数量。库存每月进行一次。所以我加入了日历表并显示日期和值。
如果我从 i.qty - s.qty 中减去
,如果我第二天再次检查库存将显示 100,那么仅在当天销售期间就会减去。
有人可以建议吗?!!
最佳答案
select i.storeno,i.datee,i.productbarcode,
isnull(i.productqty-(select sum(saleqty) as runningsum from #salestable t where t.date<=i.datee and t.storeno=i.storeno),i.productqty) as tproductqty
from #inventory i
left join
#salestable s
on s.storeno=i.storeno
and s.productbarcode=i.productbarcode
and s.date=i.datee
输出:
storeno datee productbarcode productqty
61 2016-02-28 2017961746012 100
61 2016-02-29 2017961746012 92
61 2016-03-01 2017961746012 90
61 2016-03-02 2017961746012 90
61 2016-03-03 2017960624045 90
61 2016-03-04 2017961746012 95
75 2016-03-04 2017960624045 90
61 2016-03-05 2017961746012 95
关于mysql - 如何使用带有时间戳的MySQL减去库存和销售,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38992328/