从SQL中的两个不同表中减去

时间:2013-02-18 14:04:35

标签: sql-server-2008 ms-access access-vba

由于我的问题有点特别,经过很长时间的搜索,我还没有找到并回答,所以这里有:我有两个表:In_Stock和Out_Stock。我使用以下选择:

IN_STOCK:

select 
INs.CatID as CategoryID, 
INs.SubCatID as SubcategoryID, Sum(INs.Quantity) as QuantityIN
from IN_Stock INs
group by INs.CatID, INs.SubCatID
╔════════════╦═══════════════╦════════════╗
║ CategoryID ║ SubcategoryID ║ QuantityIN ║
╠════════════╬═══════════════╬════════════╣
║          2 ║             9 ║ 0          ║
║          1 ║            16 ║ 8          ║
║          1 ║            27 ║ 5          ║
║          1 ║            30 ║ 160        ║
║          1 ║            31 ║ 6          ║
║          1 ║            39 ║ 35         ║
║          1 ║            40 ║ 7          ║
║          2 ║            44 ║ 13         ║
║          2 ║            54 ║ 6          ║
║          2 ║            70 ║ 5          ║
║          3 ║            87 ║ 3,5        ║
╚════════════╩═══════════════╩════════════╝

OUT_Stock:

select 
OUTs.CatID as CategoryID, 
OUTs.SubCatID as SubcategoryID, 
Sum(OUTs.Quantity) as QuantityOUT
from OUT_Stock OUTs
group by OUTs.CatID, OUTs.SubCatID
╔════════════╦═══════════════╦═════════════╗
║ CategoryID ║ SubcategoryID ║ QuantityOUT ║
╠════════════╬═══════════════╬═════════════╣
║          1 ║            30 ║          30 ║
║          1 ║            39 ║          15 ║
╚════════════╩═══════════════╩═════════════╝

我得到的是这张表(显然不对)。

select
INs.CatID as CategoryID, 
INs.SubCatID as SubcategoryID, 
Sum(INs.Quantity) as QuantityIN,
SUM(OUTs.Quantity) as QuantityOUT,
SUM(INs.Quantity)- SUM(OUTs.Quantity) as RemainingQuantity
from IN_Stock INs
left join OUT_Stock OUTs on INs.CatID=OUTs.CatID and INs.SubCatid=OUTs.SubCatid
group by INs.catid, INs.subcatid

╔════════════╦═══════════════╦═════════════╦════════════╦═══════════════════╗
║ CategoryID ║ SubcategoryID ║ QuantityIN  ║ QuantityOUT║ RemainingQuantity ║
╠════════════╬═══════════════╬═════════════╬════════════╬═══════════════════╣
║          2 ║             9 ║ 0           ║            ║                   ║
║          1 ║            16 ║ 8           ║            ║                   ║
║          1 ║            27 ║ 5           ║            ║                   ║
║          1 ║            30 ║ 320         ║        150 ║               170 ║
║          1 ║            31 ║ 6           ║            ║                   ║
║          1 ║            39 ║ 35          ║         30 ║                 5 ║
║          1 ║            40 ║ 7           ║            ║                   ║
║          2 ║            44 ║ 13          ║            ║                   ║
║          2 ║            54 ║ 6           ║            ║                   ║
║          2 ║            70 ║ 5           ║            ║                   ║
║          3 ║            87 ║ 3,5         ║            ║                   ║
╚════════════╩═══════════════╩═════════════╩════════════╩═══════════════════╝

我想要的是在SQL中选择返回类似下表的内容 ......我想知道是否以及如何在RemaningStock中查看: 130 其中SubcategoryID = 30且 20 ,其中SubCategoryID = 39。

╔════════════╦═══════════════╦════════════╦════════════╦═══════════════════╗
║ CategoryID ║ SubcategoryID ║ QuantityIN ║ QuantityIN ║ RemainingQuantity ║
╠════════════╬═══════════════╬════════════╬════════════╬═══════════════════╣
║          2 ║             9 ║ 0          ║            ║                   ║
║          1 ║            16 ║ 8          ║            ║                   ║
║          1 ║            27 ║ 5          ║            ║                   ║
║          1 ║            30 ║ 160        ║         30 ║               130 ║
║          1 ║            31 ║ 6          ║            ║                   ║
║          1 ║            39 ║ 35         ║         15 ║                20 ║
║          1 ║            40 ║ 7          ║            ║                   ║
║          2 ║            44 ║ 13         ║            ║                   ║
║          2 ║            54 ║ 6          ║            ║                   ║
║          2 ║            70 ║ 5          ║            ║                   ║
║          3 ║            87 ║ 3,5        ║            ║                   ║
╚════════════╩═══════════════╩════════════╩════════════╩═══════════════════╝

两个表都有一个或多个特定类别或子类别的记录 任何帮助深表感谢。非常感谢!
SQL或Access VBA代码对我有好处 PS:由于这是我的第一篇文章,请“温柔”。

2 个答案:

答案 0 :(得分:1)

您可以使用子查询来获得所需内容,例如:

SELECT *
FROM   (SELECT INs.catid         AS CategoryID,
           INs.subcatid      AS SubcategoryID,
           SUM(INs.quantity) AS QuantityIN
    FROM   in_stock INs
    GROUP  BY INs.catid,
              INs.subcatid) AS a
   LEFT JOIN (SELECT OUTs.catid         AS CategoryID,
                     OUTs.subcatid      AS SubcategoryID,
                     SUM(OUTs.quantity) AS QuantityOUT
              FROM   out_stock OUTs
              GROUP  BY OUTs.catid,
                        OUTs.subcatid) AS b
          ON ( a.subcategoryid = b.subcategoryid )
             AND ( a.categoryid = b.categoryid ); 

由此,使用MS Access中的查询设计窗口

来编辑和修改查询非常容易
SELECT a.categoryid,
   a.subcategoryid,
   a.quantityin,
   b.quantityout,
   [quantityin] - [quantityout] AS RemainingQuantity
FROM   (SELECT INs.catid         AS CategoryID,
           INs.subcatid      AS SubcategoryID,
           SUM(INs.quantity) AS QuantityIN
    FROM   in_stock INs
    GROUP  BY INs.catid,
              INs.subcatid) AS a
   LEFT JOIN (SELECT OUTs.catid         AS CategoryID,
                     OUTs.subcatid      AS SubcategoryID,
                     SUM(OUTs.quantity) AS QuantityOUT
              FROM   out_stock OUTs
              GROUP  BY OUTs.catid,
                        OUTs.subcatid) AS b
          ON ( a.subcategoryid = b.subcategoryid )
             AND ( a.categoryid = b.categoryid ); 

答案 1 :(得分:1)

主要问题是您的最终查询在初始数据集中的各个行上进行连接,然后才执行聚合,而您希望在中间总和上执行连接。

假设这个测试数据,例如:

CREATE TABLE table_in (
  id INTEGER,
  value INTEGER
  );

CREATE TABLE table_out (
  id INTEGER,
  value INTEGER
  );

INSERT INTO table_in(id, value) VALUES
  (1, 120),
  (1, 10);

INSERT INTO table_out(id, value) VALUES
  (1, 30);

您在上次查询中撰写LEFT JOIN的方式:

SELECT t1.value AS val1, t2.value AS val2
    FROM table_in t1 LEFT JOIN table_out t2 ON t1.id=t2.id;

会在聚合之前产生这些行:

ID     VAL1        VAL2
1      120         30
1      10          30

在这里,总和将给出:

ID      SUM(VAL1)    SUM(VAL2)
1       130          60

只要用于连接的条件有多行,就会发生这种情况。

您需要在聚合操作之后执行连接,因为您想要将所有输入的总和与所有输出的总和进行比较。

这可以使用子选择语句或CTEs完成。

例如:

WITH sum_in AS (
  SELECT id, SUM(value) AS all_in
     FROM table_in
  GROUP BY id
), sum_out AS (
  SELECT id, SUM(value) AS all_out
     FROM table_out
  GROUP BY id
)
SELECT t1.id, all_in, all_out, all_in - all_out
    FROM sum_in t1 LEFT JOIN sum_out t2 ON t1.id=t2.id