假设数据表如下:
OperDate
Buy
Sell
select CONVERT(varchar(11),OrderDate,120) as OrderDate,(select sum(Buy)-SUM(Sell) from Orders where OperDate<'2017-1-1')+ sum(Buy)-SUM(Sell) as GoodsNumber from Orders where OperDate>'2017-1-1' and OperDate<'2017-1-31' group by CONVERT(varchar(11),OrderDate,120)
说明:select sum(Buy)-SUM(Sell) from Orders where OperDate<'2017-1-1'
是计算1-1以前的存货数量。