股票表
Date StoreNo Code productQty
2015-06-02 61 1 -1
2015-06-02 61 2 -2
2015-06-03 61 1 -3
表#inventroy
Date StoreNo Code productQty
2015-06-01 61 1 33
2015-06-01 61 2 100
2015-06-02 61 1 32
2015-06-02 61 2 98
2015-06-03 61 1 33
2015-06-03 61 2 100
2015-06-04 61 1 31
2015-06-04 61 2 100
2015-06-05 61 1 33
2015-06-05 61 2 100
2015-06-06 61 1 33
2015-06-06 61 2 99
我做了联合所有库存和销售
Select
P.StoreNo
,p.Date
,p.ProductBarCode
,Sum (ProductQty) As productQty
from
(Select StoreNo,Date,ProductBarCode,ProductQty From #inventory
Union all
Select StoreNo,Date,ProductBarCode,ProductQty from #sale )p
Group By p.StoreNo,p.Date,p.ProductBarCode
OutPut应该像
Date StoreNo Code productQty
2015-06-01 61 1 33
2015-06-01 61 2 100
2015-06-02 61 1 32
2015-06-02 61 2 98
2015-06-03 61 1 29
2015-06-03 61 2 98
2015-06-04 61 1 30
2015-06-04 61 2 98
2015-06-05 61 1 30
2015-06-05 61 2 98
2015-06-06 61 1 30
2015-06-06 61 2 97
我想展示每日库存 - 每日销售!!但我不知道该怎么做!
我试过这个方法 但是当加入库存时,销售表中缺少一些数据。
SELECT StoreName ,p.Date as Date
,p.ProductBarCode as ProductBarCode ,isnull(i.productqty-(select ProductQty as runningsum from #Stock st where st.date<=i.date and st.storeno=i.storeno and st.ProductBarCode = i.ProductBarCode ),i.productqty) as ProductQty
From #inventory i
LEFT JOIN Store S ON S.StoreNo = i.StoreNo
Left JOIN #Stock st ON st.StoreNo = i.StoreNo and st.ProductBarCode = i.ProductBarCode and st.Date = i.Date