进销存中库存计算的疑问,帮忙看看sql该怎么写呢。 select a.BarCode 条码,a.Name 名称,a.DateInProduced 生产日期,(a.Number-SUM(b.Number)) 库存,a.WareHouse 仓库 from Inventory a,InventoryOut b where a.DateInProduced = b.DateInProduced group by b.DateInProduced,a.Number,a.BarCode,a.Name,a.DateInProduced,a.WareHouse having a.Number-SUM(b.Number)>0 order by a.DateInProduced
------解决方案-------------------- left join 应该也解决不了问题,楼主可以把公共部分取出来做一个新表,然后去left join出入库分别的汇总。
------解决方案--------------------
SQL code
select a.BarCode 条码,a.Name 名称,a.DateInProduced 生产日期,(a.Number-isnull(SUM(b.Number),0)) 库存,a.WareHouse 仓库
from Inventory a left join InventoryOut b
on a.DateInProduced = b.DateInProduced
group by b.DateInProduced,a.Number,a.BarCode,a.Name,a.DateInProduced,a.WareHouse
having a.Number-isnull(SUM(b.Number),0)>0
order by a.DateInProduced