SQLITE - 计算剩余的项目数量

时间:2013-08-02 08:15:26

标签: sqlite subtraction

我有一个库存表,其中包含特定商品ID /颜色的总商品数量:

stock_table

    item_code    |    colour    |    stock
---------------------------------------------
       A         |    red       |    10
       A         |    blue      |    8
       A         |    yellow    |    5
       B         |    red       |    5
       B         |    blue      |    0
       B         |    yellow    |    15

然后我有一个表格,其中已存储已订购的商品数量(请注意,可以在不同的订单中找到相同的商品)

orders_table

    order_id     |    item_code    |    colour    |    ordered_qty
----------------------------------------------------------------------
       1         |        A        |     red      |        3
       1         |        A        |     blue     |        1
       1         |        A        |     yellow   |        2
       1         |        B        |     red      |        1
       2         |        A        |     red      |        1
       2         |        A        |     yellow   |        3
       2         |        B        |     yellow   |        15

我需要创建一个保存剩余库存数量的表格:例如,对于库存表格中的每个商品的数量,我需要从订单表中减去“已订购”数量,得到这样的结果:

remaining_table

    item_code    |    colour    |    remaining
------------------------------------------------
       A         |    red       |    6
       A         |    blue      |    7
       A         |    yellow    |    0
       B         |    red       |    4
       B         |    blue      |    0
       B         |    yellow    |    0

我正在考虑使用视图,因为该表将为每个新插入的订单行自动更新。我能够获得每件商品/颜色的“已订购”数量:

SELECT item_code, colour, SUM(stock) as already_placed FROM orders_table GROUP BY item_code, colour

此查询将返回类似的内容(请注意,B / blue从未被订购过):

    item_code    |    colour    |    already_placed
----------------------------------------------------
       A         |    red       |    4
       A         |    blue      |    1
       A         |    yellow    |    5
       B         |    red       |    1
       B         |    yellow    |    15

现在我不得不构建一个查询,它会在表格或其他视图中为我提供 remaining_table数量 ..

长话短说,我需要从原来的 stock_table 中减去已经放置的数量,你能帮我吗?我在http://www.sqlite.org/lang_aggfunc.html

找不到任何“减号”或“减法”功能

有可能吗?

提前致谢,最好的问候

3 个答案:

答案 0 :(得分:1)

您可以联合stock_tableorders_table,但在orders_table中您可以更改ordered_qty的符号。然后你group by

这样的东西
SELECT item_code, colour, SUM(stock) as remaining FROM
(
    SELECT item_code, colour, stock FROM stock_table
    UNION ALL
    SELECT item_code, colour, -ordered_qty as stock FROM orders_table
)
GROUP BY item_code, colour

SQL小提琴:http://sqlfiddle.com/#!7/27155/1

其他选项

SELECT sto.item_code, sto.colour, sto.in_stock - COALESCE(ord.already_placed, 0) AS remaining 
    FROM
        (SELECT item_code, colour, SUM(stock) AS in_stock FROM stock_table GROUP BY item_code, colour) sto
    LEFT JOIN
        (SELECT item_code, colour, SUM(ordered_qty) AS already_placed FROM orders_table GROUP BY item_code, colour) ord
        ON ord.item_code = sto.item_code AND ord.colour = sto.colour

此处SELECTsto的{​​{1}}可以放在两个视图中(一个包含订购的库存总数,另一个包含库存库存总数。

SQL小提琴:http://sqlfiddle.com/#!7/d78d7/13

答案 1 :(得分:1)

您可以使用相关子查询来计算已订购的特定项目的数量:

CREATE VIEW remaining_view AS
SELECT item_code,
       colour,
       stock - (SELECT IFNULL(SUM(ordered_qty), 0)
                FROM orders_table
                WHERE orders_table.item_code = stock_table.item_code
                  AND orders_table.colour    = stock_table.colour)
       AS remaining
FROM stock_table

SQL Fiddle

答案 2 :(得分:1)

您可以使用自然左连接:

SELECT item_code, colour, IFNULL(q_stock - q_ordered, 0) as remaining
FROM (SELECT item_code, colour, SUM(stock) as q_stock FROM stock_table GROUP BY item_code, colour)
 NATURAL LEFT JOIN
 (SELECT item_code, colour, SUM(ordered_qty) as q_ordered FROM orders_table GROUP BY item_code, colour)