0

我们的库存存在问题,因此我们有一个余额列,每当交易发生时都会添加或减去该列。有一些我们无法追踪的问题,因此进行了更改,通过添加和减去(在适当的情况下)移动的数量来计算库存,并得出今天的库存价值。

然而,现在的结构是一种产品有多个库存,因此产品 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

4

3 回答 3

1

尝试这个:

WITH
    S AS (
        SELECT DISTINCT prodid FROM stocks
    ),
    L AS (
        /* SELECT 10 AS locationid */
        SELECT DISTINCT locationid FROM grns
    ),
    T1 AS (
        select 
            g.locationid, 
            s.prodid,
            sum(gd.qty) 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
        GROUP BY g.locationid, s.prodid
    ),
    T2 AS (
        select 
            co.locid as locationid,
            s.prodid,
            sum(cod.qty) 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'
        GROUP BY co.locid, s.prodid
    ),
    T3 AS (
        select 
            co.locid as locationid,
            s.prodid,
            sum(cod.qty) 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'
        GROUP BY co.locid, s.prodid
    )
SELECT 
    S.prodid, L.locationid, ISNULL(grnadd, 0) - ISNULL(salesub, 0) - ISNULL(cussub, 0)
FROM
    S CROSS JOIN
    L LEFT OUTER JOIN
    T1 ON T1.locationid = L.locationid AND T1.prodid = S.prodid LEFT OUTER JOIN
    T2 ON T2.locationid = L.locationid AND T2.prodid = S.prodid LEFT OUTER JOIN
    T3 ON T3.locationid = L.locationid AND T3.prodid = S.prodid

我认为位置可能也很重要,所以它返回 3 列。如果您只想要 id 为 10 的位置的结果,则在LCTL 中取消注释第一行并注释第二行。

于 2013-11-12T14:48:10.330 回答
1

尝试包含所有产品 ID 并按其分组。这是一些建议的代码

select prodid, sum(total) as total from (
       (select s.prodid
             , ISNULL(sum(gd.qty),0) as total
          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 group by s.prodid)
union all
       (select s.prodid
             , ISNULL(sum(cod.qty), 0)*(-1) as total 
          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 group by s.prodid)
union all
       (select s.prod_id
             , ISNULL(sum(cod.qty), 0)*(-1) as total
          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 group by s.prodid)
) as x
group by prodid

我已将所有缺点更改为 union-alls,这样产品 ID 就不会彼此相减,并且总计将根据您的产品 ID 进行计算。

于 2013-11-12T14:44:37.683 回答
0

假设很多关于您的数据模型(希望我理解正确的问题),agroup by可能是答案:

select
(select s.prodid, 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 
GROUP BY s.prodid)
-
(select s.prodid, 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
GROUP BY s.prodid)
-
(select s.prodid, 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
GROUP BY s.prodid)
于 2013-11-12T14:40:39.563 回答