入库详情 出库详情 计算库存的 sql

有两张表
入库详情图片说明
出库详情图片说明
怎么计算某个仓库 当前库存呢 求sql server 语句
谢谢大佬!

 select table_in.GoodsId,(table_in.Amount - table_out.Amount) from (
  select GoodsId ,Id,sum(Amount) Amount from Test_LGL_EntryInfo Group by GoodsId,Id
    ) table_in left join(
    select GoodsId ,Id,sum(Amount) Amount from Test_LGL_OutboundInfo Group by GoodsId,Id
   )table_out on table_in.GoodId = table_out.GoodsId where table_in.Id =仓库ID
select table_in.GoodsId,(table_in.Amount - table_out.Amount) from (
  select GoodsId ,sum(Amount) Amount from Test_LGL_EntryInfo Group by GoodsId
    ) table_in left join(
    select GoodsId ,sum(Amount) Amount from Test_LGL_OutboundInfo Group by GoodsId
    )table_out on table_in.GoodId = table_out.GoodsId

随便瞎写了一段,看看有没有帮助

啊哈,实际上,你需要计算的就是入库表的入库量count-出库表的出库量count,对吗

select sum(入库量)-sum(出库量) 库存量
from 入库表,出库表

有入库出库,那么肯定是有仓库表咯,那么在入库出库的时候,不就已经计算了仓库的当前库存么,即仓库表中不应该已经存在当前库存一栏么,如果说每次的当前库存都要根据入库出库表去计算的话,会搞死自己的。