使用SQL计算多个表事务的库存数量

时间:2013-11-12 14:28:34

标签: sql sql-server

我们的库存存在问题,我们有一个余额列,可以在交易发生时添加或减少。 有一些问题我们无法追踪,因此已经做出了改变,通过增加和减去(在适当的情况下)移动数量并达到今天的股票价值来计算股票。

但是现在,结构是这样的,一个产品有多个股票,因此产品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,expiry

产品表:id,商品名称

GRN详细信息表:id,grnid,qty,stockid(因为它影响股票而非产品)

销售&客户订单详细信息表:id,cusordid,qty,stockid

GRN&销售& Cus Ord表:id,locid

位置表:id,locname

3 个答案:

答案 0 :(得分: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,以便不会相互减去product-id,并且将根据您的product -ID计算总计。

答案 1 :(得分: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的位置,则在L CTL取消注释第一行并注释第二行。

答案 2 :(得分:0)

假设您的数据模型很多(希望我理解正确的问题),group 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)