库存表:
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
我想通过计算带时间戳的
(INVENTORY + ALL ADDITIONAL PRODUCTS COMING IN) - ALL SOLD PRODUCTS
来确定实际数字。库存是每月一次。所以我加入了日历表并显示日期和值。如果我是
subtract from i.qty - s.qty
,如果我再次检查第二天的库存,它将仅在当天的销售中减去。有人可以建议吗?!
最佳答案
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/