1

由于我的问题有些特殊,经过很长时间的搜索,我还没有找到并回答,所以就这样吧:我有两个表:In_Stock 和 Out_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        ║
╚════════════╩═══════════════╩════════════╝

缺货:

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:由于这是我的第一篇文章,请“温柔”。

4

2 回答 2

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 ); 
于 2013-02-18T14:13:05.907 回答
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

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

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

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

例如:

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
于 2013-02-18T14:31:43.443 回答