1

我之前找到了解决问题的方法,但不幸的是我丢失了硬盘驱动器上的文件,并且找不到我设法生成的语句。

我有 2 个表 T2REQ 和 T2STOCK,它们都有 2 列(typeID 和数量),我的问题在于我可以在两个表中多次出现相同的 typeID。

我正在尝试做的是按 typeID 分组的 SUM(QUANTITY) 并从 T2REQ 中减去 T2STOCK 的值,但是由于我在两个表中都多次出现相同的 typeID,所以我得到的 SUM 乘以 typeID 的出现次数。

这是 T2REQ 的示例(以 typeID 11399 为例):

typeID      quantity
----------  ----------
34          102900
35          10500
36          3220
37          840
11399       700
563         140
9848        140
11486       28
11688       700
11399       390
4393        130
9840        390
9842        390
11399       390
11483       19.5
11541       780

这是 T2STOCK 表的示例:

typeID      quantity
----------  ----------
9842        1921
9848        2400
11399       1700
11475       165
11476       27
11478       28
11481       34
11483       122
11476       2

这就是我现在所处的位置,我知道 SUM(t2stock.quantity) 由于 JOIN 1 = 1 而受到影响(成倍增加),但无论我尝试了什么,我都没有按照正确的顺序进行操作:

        SELECT 
           t2req.typeID, sum(t2req.quantity), sum(t2stock.quantity),
           sum(t2req.quantity) - sum(t2stock.quantity) as diff 
        FROM t2req JOIN t2stock ON t2req.typeID = t2stock.typeID 
        GROUP BY t2req.typeID 
        ORDER BY diff DESC;

typeID      sum(t2req.quantity)  sum(t2stock.quantity)  diff
----------  -------------------  ---------------------  ----------
563         140                  30                     110
11541       780                  780                    0
11486       28                   40                     -12
11483       19.5                 122                    -102.5
9840        390                  1000                   -610
40          260                  940                    -680
9842        390                  1921                   -1531
9848        140                  2400                   -2260
11399       1480                 5100                   -3620
39          650                  7650                   -7000
37          1230                 116336                 -115106
36          28570                967098                 -938528
35          33770                2477820                -2444050
34          102900               2798355                -2695455

您可以看到 typeID 11399 的 SUM(t2req) 是正确的:1480

您可以看到 typeID 11399 的 SUM(t2stock) 不正确:5100 而不是 1700(即 5100 除以 3,t2req 中的出现次数)

由于我的 sum substract 的 JOIN 有多个 typeID(在两个表中),避免乘法的最佳方法是什么?

对不起,文字墙,我只是尽力解释,因为英语不是我的母语。非常感谢你的帮助。

4

2 回答 2

1

您可以在加入前聚合:

SELECT 
   t2req.typeID, 
   t2req.quantity, 
   t2stock.quantity,
   t2req.quantity - t2stock.quantity as diff 
FROM 
    (SELECT TypeID, SUM(Quantity) Quantity FROM t2req GROUP BY TypeID) t2req JOIN 
    (SELECT TypeID, SUM(Quantity) Quantity FROM t2stock GROUP BY TypeID) t2stock 
        ON t2req.typeID = t2stock.typeID 
ORDER BY diff DESC;

小提琴样本:http ://sqlfiddle.com/#!7/06711/5

于 2013-11-12T11:54:11.080 回答
0

You can't do this in a single aggregation:

    SELECT 
       COALESCE(r.typeID, s.typeID) AS typeID,
       COALESCE(r.quantity, 0) AS req_quantity,
       COALESCE(s.quantity, 0) AS stock_quantity,
       COALESCE(r.quantity, 0) - COALESCE(s.quantity, 0) AS diff
    FROM (
            SELECT rr.typeID, SUM(rr.quantity) AS quantity 
            FROM t2req rr
            GROUP BY rr.typeID
        ) r
    CROSS JOIN (
            SELECT ss.typeID, SUM(ss.quantity) AS quantity 
            FROM t2stock ss 
            GROUP BY ss.typeID
        ) s ON r.typeID = s.typeID
    ORDER BY 4 DESC;
于 2013-11-12T11:50:43.340 回答