1

我对这种复杂的连接和求和函数非常陌生。我有 2 个具有相同字段(i.e: ProdID, ProdName, NetQty, Unit) 的表查询。第一个查询包含OpStock,第二个查询包含Purchase

我想添加这 2 个表查询以创建一个表,以便我能够查看当前库存。

第一个表的数据如下所示:

ProdID ProdName Qty
100 Rose     700
101 Lilly    550
103 Jasmine  600
105 Lavender 400

第二张表的数据如下所示:

ProdID ProdName Qty
100 Rose     400
101 Lilly    250
104 Lotus   1000
106 MariGold 400

第三张表的最终数据如下所示:

ProdID ProdName Qty
100 Rose     1100
101 Lilly     800
103 Jasmine   600
104 Lotus    1000
105 Lavender  400
106 MariGold  400

我如何使用 sql for access2003 来实现这一点。谢谢。

我很抱歉 Ciaran,这纯粹是用于 vb.net 的访问 这是我的访问查询 1

SELECT sp_OpenIandP_Prod.ProdID,
       sp_OpenIandP_Prod.ProdName,
       Sum(([sp_OpenIandP_Prod.SumOfNetQty]-[sp_OpenSales_Prod.SumOfNetQty])) AS NetQty,
       sp_OpenIandP_Prod.UnitSName
FROM sp_OpenIandP_Prod 
     INNER JOIN sp_OpenSales_Prod ON sp_OpenIandP_Prod.ProdID=sp_OpenSales_Prod.ProdID
GROUP BY sp_OpenIandP_Prod.ProdID, 
         sp_OpenIandP_Prod.ProdName, 
         sp_OpenIandP_Prod.UnitSName;

第一个查询结果是这样的:

ProdID  ProdName    NetQty  UnitSName
1   Rose            0   Kgs
2   Lilly       7125    Mts
3   Lotus       12374   Nos

第二个查询是:

SELECT Products.ProdID, Products.ProdName,
       Sum(OPDDetails.NetQty) AS SumOfNetQty, Units.UnitSName
FROM Units 
     INNER JOIN (Products 
         INNER JOIN (Brands 
             INNER JOIN OPDDetails ON Brands.BrID=OPDDetails.BrandID)
         ON Products.ProdID=Brands.ProdID) 
     ON Units.UnitID=Products.UnitID
WHERE (((OPDDetails.PurID)>0) 
  AND ((OPDDetails.OPDDate)>=[StartDate] And (OPDDetails.OPDDate)<=[EndDate]))
GROUP BY Products.ProdID, Products.ProdName, Units.UnitSName;

结果集是这样的:

ProdID  ProdName    SumOfNetQty UnitSName
1   Rose    1800    Kgs
2   Lilly   21000   Mts

我很容易得到上述结果。但要得到这样的结果并不容易:

ProdID  ProdName    SumOfNetQty UnitSName
1   Rose    1800    Kgs
2   Lilly   28125   Mts
3       Lotus   12374   Nos

就这样。再次感谢。

4

2 回答 2

1

这不是 vb.net 问题,但是您需要使用 UNION

Select ProdId, ProdName, Sum(Qty) As QtySum
  From (Select ProdId, ProdName, Qty From TableA
        Union All
        Select ProdId, ProdName, Qty From TableB) DerrivedView
 Group By ProdId, ProdName
于 2012-12-17T11:25:33.280 回答
0

您可以在 2 个查询之间创建一个 UNION,然后按产品分组:

SELECT ProdID, ProdName, Sum(NetQty) As NetQty, UnitSName
FROM 
( 

    SELECT sp_OpenIandP_Prod.ProdID,
           sp_OpenIandP_Prod.ProdName,
           Sum(([sp_OpenIandP_Prod.SumOfNetQty]-[sp_OpenSales_Prod.SumOfNetQty])) AS NetQty,
           sp_OpenIandP_Prod.UnitSName
    FROM sp_OpenIandP_Prod 
         INNER JOIN sp_OpenSales_Prod ON sp_OpenIandP_Prod.ProdID=sp_OpenSales_Prod.ProdID
    GROUP BY sp_OpenIandP_Prod.ProdID, 
             sp_OpenIandP_Prod.ProdName, 
             sp_OpenIandP_Prod.UnitSName

    UNION ALL

    SELECT Products.ProdID, Products.ProdName,
           Sum(OPDDetails.NetQty) AS NetQty, Units.UnitSName
    FROM Units 
         INNER JOIN (Products 
             INNER JOIN (Brands 
                 INNER JOIN OPDDetails ON Brands.BrID=OPDDetails.BrandID)
             ON Products.ProdID=Brands.ProdID) 
         ON Units.UnitID=Products.UnitID
    WHERE (((OPDDetails.PurID)>0) 
      AND ((OPDDetails.OPDDate)>=[StartDate] And (OPDDetails.OPDDate)<=[EndDate]))
    GROUP BY Products.ProdID, Products.ProdName, Units.UnitSName

)
GROUP BY ProdID, ProdName, UnitSName
于 2012-12-17T12:28:21.510 回答