我们的库存存在问题,因此我们有一个余额列,每当交易发生时都会添加或减去该列。有一些我们无法追踪的问题,因此进行了更改,通过添加和减去(在适当的情况下)移动的数量来计算库存,并得出今天的库存价值。
然而,现在的结构是一种产品有多个库存,因此产品 A 的到期日为 01/2012、02/2013 等。
我目前创建了一个查询,对于一种产品的库存,它将按如下方式计算其当前库存:
select
(select ISNULL(sum(gd.qty),0) as grnadd from grns as g
INNER JOIN grndetails as gd ON g.id = gd.grnid
INNER JOIN stocks as s ON s.id = gd.stockid
where g.locationid = 10 and s.prodid =2653)
-
(select ISNULL(sum(cod.qty), 0) as salesub from salesorders as co
INNER JOIN salesorddetails as cod ON co.id = cod.cusordid
INNER JOIN stocks as s ON s.id = cod.stockid
where co.status != 'cancel' and co.locid = 10 and s.prodid =2653)
-
(select ISNULL(sum(cod.qty), 0) as cussub from customerorders as co
INNER JOIN customerorddetails as cod ON co.id = cod.cusordid
INNER JOIN stocks as s ON s.id = cod.stockid
where co.status != 'cancel' and co.locid = 10 and s.prodid =2653)
因此,在这种情况下,库存是针对一种产品计算的,但是我可以进行查询,在第二列中列出所有产品及其总数(如上)吗?
谢谢,希望从上面的查询中可以理解结构
编辑:
股票表:id、prodid、到期
产品表:id、tradename
GRN 详细信息表:id、grnid、qty、stockid(因为它影响库存而不是产品)
销售和客户订单明细表:id、cusordid、qty、stockid
GRN & Sales & Cus Ord 表:id、locid
位置表:id、locname