Sqlite减法求和(带分组)与JOIN和重复

时间:2013-11-12 11:43:15

标签: sql sqlite join sum

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

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

我要做的是按照typeID分组的SUM(QUANTITY)并从T2REQ中减去T2STOCK的值,但由于我在两个表中都有多个相同typeID的出现,因此我获得的SUM乘以出现的次数of 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中出现的次数)

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

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您无法在单个聚合中执行此操作:

    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;